Reputation: 359
I have two WPF elements bound to the same ObservableCollection. One is a Datagrid and one is a ListBox. When the Datagrid is used to sort on a column (using the built-in column headers) the action puts the items in the listbox into the same order. In other words, it seems that the sorting action in the Datagrid affects the ordering of the underlying collection. Is there a way to disable this behavior?
Here is the XAML for the Datagrid:
<DataGrid
IsReadOnly="True">
>
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding no}" >
<DataGridTextColumn.Header>
<TextBlock>
File<LineBreak/>No.
</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn
Header="Name"
Binding="{Binding fileName}" />
<DataGridTextColumn Binding="{Binding channels}" >
<DataGridTextColumn.Header>
<TextBlock TextAlignment="Center">
Channels<LineBreak/>[#]
</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
and the Listbox:
<ListBox
SelectedIndex="{Binding fileListSelectedIndex}"
SelectedItem="{Binding fileListSelectedItem}"
>
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:FileListItem}">
<TextBlock Text="{Binding Path=fileName}"/>
</DataTemplate>
</ListBox.Resources>
</ListBox>
finally, the binding code:
filelist.ItemsSource = vm.fileList;
multiFileParamGrid.ItemsSource = vm.fileList;
Upvotes: 1
Views: 84
Reputation: 35681
ItemsSource uses special wrapper type (ICollectionView
) when binding to some sequence or collection. That wrapper provides sorting functionality. The default wrapper object is obtained from CollectionViewSource.GetDefaultView
method. When two ItemsControls (DataGrid and ListBox) bind to the same collection (vm.fileList
), they (and any other code) will receive the same wrapper object.
But it is possible to create a different instance of wrapper on purpose:
filelist.ItemsSource = vm.fileList;
multiFileParamGrid.ItemsSource = new ListCollectionView(vm.fileList);
Upvotes: 2