我想处理WPF DataGrid元素中的SelectionChanged事件,仅用于用户交互/选择,如果是由于绑定或其他设置值而跳过。知道如何确定选择是否会因用户交互而改变吗?或者任何可以完成类似任务的备用事件?
发布于 2013-01-13 17:06:21
也许可以尝试将SelectionChanged事件与PreviewMouseDown事件结合使用。当用户单击一行时,您设置了一些属性,并在SelectionChanged事件处理程序中检查是否更改了than属性。
示例代码XAML:
<DataGrid SelectionChanged="OnSelectionChanged" PreviewMouseDown="OnPreviewMouseDown">
<!--some code-->
</DataGrid>代码隐藏:
bool isUserInteraction;
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (isUserInteraction)
{
//some code
isUserInteraction = false;
}
}
private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
isUserInteraction = true;
}发布于 2017-11-02 17:24:10
您好,您可以在xaml中使用以下代码:
<ComboBox x:Name="ComboBoxName" SelectionChanged="ComboBox_SelectionChanged">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="IsDropDownOpen" Value="True">
<Setter Property="IsEditable" Value="True"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>在后面的代码中:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!((ComboBox)sender).IsEditable) return;
//Do Stuff;
}发布于 2018-02-26 02:03:27
另一种方法是处理ComboBox的DropDownOpened和DropDownClosed事件。这比Rafal接受的答案稍微好一点,因为它防止了如果用户单击ComboBox,然后单击其他地方,导致ComboBox在没有做出选择的情况下关闭时,布尔标志被卡住为真。但是,它不能解决的是,如果ComboBox有键盘焦点,并且用户点击向上和向下箭头来更改选择。
private void Event_ComboBox_DropDownOpened(object sender, EventArgs e)
{
isUserInteraction = true;
}
private void Event_ComboBox_DropDownClosed(object sender, EventArgs e)
{
isUserInteraction = false;
}
private void Event_ComboBox_SelectedChanged(object sender, SelectionChangedEventArgs e)
{
if (isUserInteraction)
{
// Do work
}
}https://stackoverflow.com/questions/14301271
复制相似问题