每次我在TabControl中切换选项卡时,所选项目都会被重置

本文关键字:选项 项目 TabControl | 更新日期: 2023-09-27 18:06:15

我有以下TabControl:

<TabControl Name="tabControl" Grid.Row="0" MinWidth="270" HorizontalAlignment="Stretch" ItemsSource="{Binding Counters}" ContentTemplate="{StaticResource templateForTheContent}"
        ItemTemplate="{StaticResource templateForTheHeader}">
</TabControl>

它使用这个DataTemplate:

<Window.Resources>
            <DataTemplate x:Key="templateForTheContent" >
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*"/>
                    <RowDefinition  Height="Auto"/>
                </Grid.RowDefinitions>
                <ListBox Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" 
                         ItemsSource="{Binding}"
                         SelectionMode="Multiple"
                             BorderThickness="1" BorderBrush="#FF8B8B8B" SelectionChanged="ListBox_SelectionChanged_1">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock>
                                <Run Text="{Binding CounterName, Mode=OneWay}" />
                                <Run Text="{Binding InstanceName, Mode=OneWay}" />
                            </TextBlock>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <Button Name="RAMSelectAllButton" Margin="0,10,0,0" Grid.Column="0" Grid.Row="1">
                    <TextBlock Text="SELECT ALL"/>
                </Button>
                <Button Name="RAMUnSelectAllButton" Margin="0,10,0,0" Grid.Column="1" Grid.Row="1">
                    <TextBlock Text="UNSELECT ALL"/>
                </Button>
            </Grid>
        </DataTemplate>
            <DataTemplate x:Key="templateForTheHeader" >
                <TextBlock Text="{Binding CategoryName}"/>
            </DataTemplate>
        </Window.Resources>

按预期工作,绑定工作得很好,如果这个问题不存在,一切都会很好:每次我在TabControl中切换一个选项卡时,前一个选项卡中ListBox的选定项都会被重置——所以当我回到那个选项卡时,什么都没有被选中。

如何解决这个问题?

//编辑这是我的ListBox_SelectionChanged_1方法:

private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    System.Windows.Controls.ListBox listBoxTemp = sender as System.Windows.Controls.ListBox;
    PerformanceCounter counterTemp = (PerformanceCounter)listBoxTemp.Items[0];
    if (!appData.SelectedCounters.ContainsKey(counterTemp.CategoryName))
        appData.SelectedCounters.Add(counterTemp.CategoryName, new List<PerformanceCounter>());
    appData.SelectedCounters[counterTemp.CategoryName].Clear();
    foreach (PerformanceCounter counter in listBoxTemp.SelectedItems)
    {
        appData.SelectedCounters[counterTemp.CategoryName].Add(counter);
    }
}

ListBox被绑定到Counters,它是Observable Collection:

public ObservableCollection<ObservableCollection<PerformanceCounter>> Counters
{
    get { return _Counters; }
}
ObservableCollection<ObservableCollection<PerformanceCounter>> _Counters = new ObservableCollection<ObservableCollection<PerformanceCounter>>();

每次我在TabControl中切换选项卡时,所选项目都会被重置

(我不熟悉TabControls,或WPF的问题,但我会建议这样的解决方案:)

在字典中备份列表框中的选定项。

var selectionBackups = new Dictionary<ListBox, IEnumerable<ListBoxItem>>();

我会在ListBox_SelectionChanged_1()中更新此字段。当您输入Tab时,覆盖相关ListBox的选择。

void TabEnter(object sender, TabEventArgs e)
{
    ListBox lb = ... //acquire the current Listbox
    OverwriteSelection(lb, selectionBackups[lb]); //set the ListBox's selection
}

(当您恢复选择时,您可能希望防止ListBox_SelectionChanged_1()触发。可以这样做:

bool auto_select = false; //set this to true while editing selections
private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    if(auto_select)
        return;
    ...
}

)