首页 > 解决方案 > TabControl 事件 SelectionChanged 在同一项目上更改时执行了两次

问题描述

在我的程序中,我动态创建 TabItems,并且可以创建一些相同的项目。问题是当我从一个切换到第二个相同的项目时。在这一刻事件中:

private void TabCards_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.Source is TabControl)
        {
            ...
        }
    }

跑2次。我可以在短时间内给秒表和阻止运行此事件两次。但我不认为这是一个好的解决方案。

也许有人知道我能做什么?

编辑

这就是我添加项目的方式:

private void AddTabItemForCard(string cardName)
    {
        var style = (Style)resources.FindResource("Style" + cardName);
        var tabItem = new TabItem
        {
            Style = style,
            Header = cardName
        };
        TabCards.Items.Add(tabItem);
    }

这是 xaml 中的 TabControl:

<TabControl x:Name="TabCards" Grid.Column="1" SelectionChanged="TabCards_SelectionChanged" >
    </TabControl>

和风格:

        <Style x:Key="StyleWej" TargetType="{x:Type TabItem}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="AllowDrop" Value="True"/>
        <EventSetter Event="PreviewMouseMove" Handler="TabItem_PreviewMouseMove"/>
        <EventSetter Event="Drop" Handler="TabItem_Drop"/>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid>
                        <TextBlock TextWrapping="Wrap" Text="Adres(hex)" FontSize="15" Margin="10,10,396,444"/>
                        <ComboBox x:Name="AddressWej" HorizontalAlignment="Left" Margin="134,11,0,0" VerticalAlignment="Top" Width="120">
                            <ComboBoxItem>30</ComboBoxItem>
                            <ComboBoxItem>31</ComboBoxItem>
                            <ComboBoxItem>32</ComboBoxItem>
                        </ComboBox>

                        <TextBlock TextWrapping="Wrap" Text="Wejscie 1:" FontSize="15" Margin="10,126,396,337"/>
                        <TextBlock TextWrapping="Wrap" Text="Wejscie 2:" FontSize="15" Margin="10,156,396,307"/>

                        <TextBox TextWrapping="Wrap" Text="Wejscie1" Width="120" Margin="134,129,231,331"/>
                        <TextBox TextWrapping="Wrap" Text="Wejscie2" Width="120" Margin="134,157,231,302"/>

                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

事件:

 private void TabItem_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (!(e.Source is TabItem tabItem))
            return;

        if (Mouse.PrimaryDevice.LeftButton == MouseButtonState.Pressed)
        {
            DragDrop.DoDragDrop(tabItem, tabItem, DragDropEffects.All);
        }
    }
    private void TabItem_Drop(object sender, DragEventArgs e)
    {
        var tabItemTarget = e.Source as TabItem;

        var tabItemSource = e.Data.GetData(typeof(TabItem)) as TabItem;

        if (!tabItemTarget.Equals(tabItemSource))
        {
            var tabControl = tabItemTarget.Parent as TabControl;
            int sourceIndex = tabControl.Items.IndexOf(tabItemSource);
            int targetIndex = tabControl.Items.IndexOf(tabItemTarget);

            tabControl.Items.Remove(tabItemSource);
            tabControl.Items.Insert(targetIndex, tabItemSource);

            tabControl.Items.Remove(tabItemTarget);
            tabControl.Items.Insert(sourceIndex, tabItemTarget);
        }
    }

在 TabCards_SelectionChanged 我用 TabItem 检查它并运行:

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject rootObject) where T : DependencyObject
    {
        if (rootObject != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(rootObject); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(rootObject, i);

                if (child != null && child is T)
                    yield return (T)child;

                foreach (T childOfChild in FindVisualChildren<T>(child))
                    yield return childOfChild;
            }
        }
    }

从 TabItem 中查找所有 TextBox

在此处输入图像描述

标签: c#wpf

解决方案


推荐阅读