问题描述
我有包含帧每个标签选项卡式界面。在这些帧中的一个有一个DataGrid。当用户选择该选项卡,我需要我的DataGrid排序,所以我用的TabControl SelectionChanged事件触发排序。然而,这一事件将触发每一个项目从DataGrid中选定的时间,即使标签本身保持不变。
I have a tabbed GUI with each tab containing a Frame. In one of these Frames there is a DataGrid. When the user selects this tab, I need my datagrid sorted, so I'm using the TabControl SelectionChanged event to trigger the sort. However, this event triggers every time an item is selected from the DataGrid, even though the tabs themselves remain untouched.
我已经尝试了不同的事件数:
的GotFocus一个TabItem的
RequestBringIntoView一个TabItem的
I've tried number of different events:GotFocus for a TabItemRequestBringIntoView for a TabItem
但他们似乎都存在这个问题受到影响。是什么原因造成的?
but they all seem to suffer from this problem. What is causing this?
推荐答案
的 TabControl.SelectionChanged
是相同的事件作为 ComboBox.SelectionChanged
从<$c$c>Selector.SelectionChanged$c$c>.
所以,如果你在你的事件处理程序处理,不标记您的活动,它会冒泡的树,并最终在你的的TabControl
,这是造成该到达射击往往的问题。
So, if you do not mark your event as handled in your event handler, it will bubble up the tree, and eventually arrive at your TabControl
, which is causing this "firing too often" issue.
标记您的活动作为你的组合框
/ 的ListBox
/ 的ListView的SelectionChanged处理
/你在DataGrid中使用像这样的任何其他的选择:
Mark your event as handled in your SelectionChanged of your ComboBox
/ListBox
/ListView
/any other Selector you use in your DataGrid like so:
private void MyComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
e.Handled = true;
}
和这种不便将消失。)
这篇关于在C#WPF,为什么我的TabControl的SelectionChanged事件触发过于频繁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!