首页 文章

列表框所选项目每次切换时都会重置TabControl

提问于
浏览
0

我有以下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>>();

1 回答

  • 1

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

    在Dictionary中备份ListBox的选定项目 .

    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;
        ...
    }
    

相关问题