简体   繁体   English

SelectionChanged 也在嵌套控件上触发?

[英]SelectionChanged fired also on nested controls?

Sorry for misleading title, I'll try to explain better.抱歉误导标题,我会尽量解释得更好。 I've a TabControl like this:我有一个这样的 TabControl:

<dragablz:TabablzControl SelectionChanged="MainTabs_SelectionChanged" x:Name="MainTabs">

where inside I've different TabItems , I need to fire the event MainTabs_SelectionChanged each time the user change the TabItem , this working but the event is fired also when the selection of a combobox, available inside the tabitem, change.在里面有不同的TabItems ,我需要每次用户更改TabItem时启动事件MainTabs_SelectionChanged ,这也是在选举组合框中选择的那样,在TabItem内部触发,更改。

This is the ComboBox :这是ComboBox

<ComboBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding Groups}" 
                                              Margin="8,0,8,16" DisplayMemberPath="Name" SelectedItem="{Binding SelectedGroup}" />

why happen this?为什么会这样?

why happen this?为什么会这样?

Because SelectionChanged is a routed event.因为SelectionChanged是一个路由事件。

Routed Events Overview: https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/routed-events-overview路由事件概述: https : //docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/routed-events-overview

You could use the OriginalSource property to determine whether a tab was selected:您可以使用OriginalSource属性来确定是否选择了选项卡:

private void MainTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.OriginalSource == MainTabs)
    {
        //do your thing
    }
}

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

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