我有一个 ItemsControl
,其 ItemsPresenter
响应 MouseMove
事件。项目在数据源内移动,如果鼠标位于控件上方时移动项目,则会导致 MouseMove
事件触发,即使鼠标没有移动。
以下是演示问题的示例。
XAML:
<ItemsControl Name="ladder" ItemsSource="{Binding Rows}">
<ItemsControl.Template>
<ControlTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Header" Grid.Column="0" />
<ItemsPresenter Grid.Row="1"
MouseMove="OnMouseMove"/>
</Grid>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
C#语言:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
Rows.Add(new Row { Name = "0" });
Rows.Add(new Row { Name = "1" });
Rows.Add(new Row { Name = "2" });
Rows.Add(new Row { Name = "3" });
Rows.Add(new Row { Name = "4" });
DispatcherTimer t = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(1000) };
t.Tick += T_Tick;
t.Start();
}
private void T_Tick(object sender, EventArgs e)
{
Rows.Move(4, 0);
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
Debug.WriteLine(e.Timestamp);
}
public ObservableCollection<Row> Rows { get; set; } = new ObservableCollection<Row>();
}
public class Row
{
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
如果你在调试/运行时将鼠标移到
ItemsControl
上并停留一段时间,你会在输出窗口中看到控件中的项移动时触发了 MouseMove
事件。这是为什么呢?是否有办法过滤这些事件,只响应“真正”的鼠标移动事件?