简体   繁体   English

ComboBoxItem选择将触发TabControl_SelectionChanged事件

[英]ComboBoxItem selection fires the TabControl_SelectionChanged Event

I've a ComboBox inside a TabItem. 我在TabItem中有一个ComboBox。 The problem is that when I select any ComoboxItem, the TabControl_SelectionChanged Event is fired. 问题是,当我选择任何ComoboxItem时,都会触发TabControl_SelectionChanged事件。 and I have some Functions inside that event that I don't want it to be implemented once i change the ComboBox selected item. 而且我在事件中有一些函数,一旦更改ComboBox选定的项目,我就不希望实现它。

 <TabControl x:Name="tb" HorizontalAlignment="Left" Height="299" Margin="10,10,0,0" VerticalAlignment="Top" Width="497" SelectionChanged="TabControl_SelectionChanged">
            <TabItem x:Name="tbi1" Header="TabItem">
                <Grid Background="#FFE5E5E5">
                    <Label x:Name="lbl" Content="Label" Margin="196,86,172,148"/>
                    <ComboBox HorizontalAlignment="Left" Margin="51,162,0,0" VerticalAlignment="Top" Width="120">
                        <ComboBoxItem Content="ComboBoxItem"/>
                        <ComboBoxItem Content="ComboBoxItem"/>
                        <ComboBoxItem Content="ComboBoxItem"/>
                    </ComboBox>
                </Grid>
            </TabItem>
            <TabItem x:Name="tbi2" Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>

Edit: Also i face that problem with hovering any control within the Tab item as it hovers the tabitem too. 编辑:同样,我也面临着将鼠标悬停在Tab项上时,将任何控件悬停在Tab项内的问题。

Set e.Handled to True in SelectionChanged event of ComboBox. 在ComboBox的SelectionChanged事件中将e.Handled设置为True。

  private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            e.Handled = true;
        }

ComboBox and TabControl are derived from Selector, and SelectionChanged event is a routed event, so the child ComboBox' SelectionChanged will be routed to parent control TabControl. ComboBox和TabControl派生自Selector,而SelectionChanged事件是一个路由事件,因此子ComboBox的SelectionChanged将被路由到父控件TabControl。 This is the WPF routed event behaviour. 这是WPF路由事件的行为。 Routed event bubble routing is accroding to the logical tree, if you put a ComboBox in a TabItem of a TabControl, when the ComboBox.SelectionChanged event raised, the event will be routed to the TabControl. 如果将ComboBox放在TabControl的TabItem中,则路由事件气泡路由将累积到逻辑树中,当ComboBox.SelectionChanged事件引发时,该事件将被路由到TabControl。 But, if the ComboBox is not in the logical tree of TabControl, then the event will not be routed to the TabControl. 但是,如果ComboBox不在TabControl的逻辑树中,则该事件将不会路由到TabControl。

Update You can check the object which fired the event in TabControl SelectionChanged event: 更新您可以在TabControl SelectionChanged事件中检查触发该事件的对象:

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.OriginalSource == tb)
        {

        }
    }

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

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