简体   繁体   English

选择tabitem时更好地处理事件

[英]Better approach for event when tabitem is selected

I have a tabcontrol with 8 tabitems and many many datagrids and listbox inside them. 我有一个tabcontrol,其中有8个tabitems,里面有很多datagrids和listbox。 I want to fire up an event only when one specific tabitem is selected. 我只想在选择一个特定的tabitem时启动一个事件。

The first approach is SelectionChanged in tabcontrol with an if statement inside it 第一种方法是tabcontrol中的SelectionChanged,其中包含if语句

If ((thetabiwant!=null)&& (thetabiwant.IsSelected)) 
{
//code here 
}

The second approach is to have a mouseup event in the desired tabitem. 第二种方法是在所需的tabitem中有一个mouseup事件。

What is the best approach? 什么是最好的方法?

(ups and downs is that SelectionChanged fires all the time because of the datagrids while the mouseup event solution doesn't make me happy) (起伏不定的是SelectionChanged因为datagrids而一直触发,而mouseup事件解决方案并没有让我开心)

Thanks. 谢谢。

In general, you shouldn't worry too much about fired events that you then skip because they don't match your criteria. 一般情况下,您不应过于担心因为与您的条件不符而跳过的事件。 The framework itself does that a lot , and you will end up doing a lot of that too (for example when listening to INotifyPropertyChanged events). 框架本身做了很多 ,你最终也会做很多事情(例如,当听取INotifyPropertyChanged事件时)。

In your case, the few additional SelectionChanged events that get fired are really negligible. 在您的情况下,触发的几个额外的SelectionChanged事件实际上可以忽略不计。 Each event requires the user to actually change the tab, and that won't happen to often. 每个事件都要求用户实际更改选项卡,这种情况不会经常发生。 On the other hand, once you are in the tab you care for, there are actually a lot mouse events happening. 另一方面,一旦你在你关心的标签中,实际上发生了很多鼠标事件。 Not that you need to care about the number of those either (you really shouldn't unless you get problems) but of course you can avoid it. 并不是说你需要关心它们的数量(除非你遇到问题,否则你真的不应该这样)但当然你可以避免它。

So in this case, yes, just skipping the SelectionChanged event is the best approach. 所以在这种情况下,是的,只是跳过SelectionChanged事件是最好的方法。

You could also bind the IsSelected Property 您还可以绑定IsSelected属性
In the set do what you need to do when it is changed to true 在集合中执行更改为true时需要执行的操作

TabItem.IsSelected TabItem.IsSelected

<TabControl Grid.Row="0">
    <TabItem Header="One" IsSelected="{Binding Path=Tab1Selected, Mode=TwoWay}"/>
    <TabItem Header="Two" IsSelected="{Binding Path=Tab2Selected, Mode=TwoWay}"/>
</TabControl>

private bool tab1Selected = true;
private bool tab2Selected = false;
public bool Tab1Selected
{
    get { return tab1Selected; }
    set
    {
        if (tab1Selected == value) return;
        tab1Selected = value;
        NotifyPropertyChanged("Tab1Selected");
    }
}
public bool Tab2Selected
{
    get { return tab2Selected; }
    set
    {
        if (tab2Selected == value) return;
        tab2Selected = value;
        if (tab2Selected)
        {
            MessageBox.Show("Tab2Selected");
            // do your stuff here
        }
        NotifyPropertyChanged("Tab2Selected");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM