ListBox Selected Items are reset each time I switc

2019-09-14 00:38发布

问题:

I have the following TabControl:

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

It uses this 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>

It works as expected, binding works well, everything would be totally fine if this issue wasn't present: Each time I switch a tab in my TabControl, selected items of a ListBox in my previous tab are reset - so when I go back to that tab - nothing is selected.

How to fix this?

//EDIT here's my ListBox_SelectionChanged_1 method:

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 is bound to Counters, which is Observable Collection:

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

回答1:

(i am not familiar with TabControls, or WPF for that matter, but i would suggest a solution like this:)

Backup the selected items of your ListBox in a Dictionary.

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

I'd keep this field updated in ListBox_SelectionChanged_1(). Whenever you enter a Tab, overwrite the concerned ListBox's selection.

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

(You might want to prevent the ListBox_SelectionChanged_1() from triggering when you restore the selection. That could be done like this:

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

)