Maintaining ListBox selection after CollectionViewSource sort in Silverlight
I have a ListBox, described below, which is sorted by radio buttons and whose items are 'selected' according to a binding to their underlying object:

Listbox:

            <ListBox x:Name="listbox" ItemsSource="{Binding Source={StaticResource DataView}}" SelectionMode="Multiple">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Padding="5,0,5,0" Text="{Binding Name}"/>
                            <TextBlock Padding="5,0,5,0" Text="{Binding X}"/>
                            <TextBlock Padding="5,0,5,0" Text="{Binding Y}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                    </Style>
                </ListBox.ItemContainerStyle>

Sort:

        <CollectionViewSource x:Key="DataView"
                        Source="{Binding Source={StaticResource ViewModel},
                             Path=Data, Mode=TwoWay}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Name"
                                     Direction="Ascending"/>
                <scm:SortDescription PropertyName="X"
                            Direction="Ascending" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

Radio buttons:

            <StackPanel Orientation="Horizontal">
                <RadioButton Name="sortname"
                     Tag="Name"
                     Checked="SortTheData"
                     Content="Sort by Name" />
                <RadioButton Name="sortx"
                     Tag="X"
                     Checked="SortTheData"
                     Content="Sort by X" />
            </StackPanel>


When you change the sort radio button, the CollectionViewSource sorts properly, except that it changes selection, which (as a requirement) is bound to the underlying datapoint. Thus, sorting changes the state of the objects in the collection, which is bad.

What is a viable approach that prevents the selection from changing, and thus prevents the underlying object from changing?

I would prefer to use datagrid, except that you can't bind selection to an IsSelected property like I did with the listbox. If someone knew a way, that would be worth hearing too. I've asked about that [here][1] in more depth.


  [1]: http://stackoverflow.com/questions/11299450/highlight-select-datagridrow-based-on-object-property-in-silverlight
Maintaining ListBox selection after CollectionViewSource sort in Silverlight
===

I have a ListBox, described below, which is sorted by radio buttons and whose items are 'selected' according to a binding to their underlying object:

Listbox:

            <ListBox x:Name="listbox" ItemsSource="{Binding Source={StaticResource DataView}}" SelectionMode="Multiple">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Padding="5,0,5,0" Text="{Binding Name}"/>
                            <TextBlock Padding="5,0,5,0" Text="{Binding X}"/>
                            <TextBlock Padding="5,0,5,0" Text="{Binding Y}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                    </Style>
                </ListBox.ItemContainerStyle>

Sort:

        <CollectionViewSource x:Key="DataView"
                        Source="{Binding Source={StaticResource ViewModel},
                             Path=Data, Mode=TwoWay}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Name"
                                     Direction="Ascending"/>
                <scm:SortDescription PropertyName="X"
                            Direction="Ascending" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

Radio buttons:

            <StackPanel Orientation="Horizontal">
                <RadioButton Name="sortname"
                     Tag="Name"
                     Checked="SortTheData"
                     Content="Sort by Name" />
                <RadioButton Name="sortx"
                     Tag="X"
                     Checked="SortTheData"
                     Content="Sort by X" />
            </StackPanel>


When you change the sort radio button, the CollectionViewSource sorts properly, except that it changes selection, which (as a requirement) is bound to the underlying datapoint. Thus, sorting changes the state of the objects in the collection, which is bad.

What is a viable approach that prevents the selection from changing, and thus prevents the underlying object from changing?

I would prefer to use datagrid, except that you can't bind selection to an IsSelected property like I did with the listbox. If someone knew a way, that would be worth hearing too. I've asked about that [here][1] in more depth.


  [1]: http://stackoverflow.com/questions/11299450/highlight-select-datagridrow-based-on-object-property-in-silverlight