ListBox实现原理
它继承于Selector利用MouseEventArgs与MouseButtonEventArgs在点击时会将点击的Item设置为SelectItem,然后左键按住并且移动时会将鼠标下的Item设置为SelectItem。
具体方法
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
this.SelectedItem = e.Source;
base.OnMouseLeftButtonDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if(e.LeftButton== MouseButtonState.Pressed)
{
this.SelectedItem = e.Source;
}
base.OnMouseMove(e);
}