Datagrid column sorting problem, sort looks fine visually but the ItemSource is still the same
Here is how i defined the DataGrid
<toolkit:DataGrid
Height="{Binding ElementName=parentCanvas, Path=ActualHeight}"
Width="{Binding ElementName=parentCanvas, Path=ActualWidth}"
SelectionMode="Single"
ScrollViewer.VerticalScrollBarVisibility="Auto"
SelectedIndex="{Binding CurrentSelectedIdx}"
ItemsSource="{Binding Path=GameHostDataList, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
AutoGenerateColumns="False"
x:Name="gamehostsDataGrid"
IsReadOnly="True"
Visibility="{Binding Path=GhListVisibility}">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Binding="{Binding FacilityId}" Header="Facility ID" MinWidth="45" Width="45*" IsReadOnly="True" SortMemberPath="FacilityId"/>
<toolkit:DataGridTextColumn Binding="{Binding FacilityName}" Header="Facility Name" MinWidth="100" Width="110*" IsReadOnly="True" SortMemberPath="FacilityName"/>
<toolkit:DataGridTextColumn Binding="{Binding GameHostIp}" Header="GH IP" MinWidth="70" Width="75*" IsReadOnly="True" SortMemberPath="GameHostIp"/>
<toolkit:DataGridTextColumn Binding="{Binding Status}" Header="Status" MinWidth="80" Width="85*" IsReadOnly="True" SortMemberPath="Status"/>
<toolkit:DataGridTemplateColumn Header="" Width="Auto" MinWidth="24">
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" ToolTip="Delete"
Command="{StaticResource deleteGhCommand}" Focusable="False"
Width="24" Height="24">
<Image Source="pack://application:,,,/DesktopShell;component/Resources/Buttons/Aha-Soft/No-entry.png" />
</Button>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
</toolkit:DataGrid.Columns>
<e:Interaction.Triggers>
<e:EventTrigger EventName ="SelectionChanged">
<b:CommandAction Command="{Binding DisplayGhCommand}"/>
</e:EventTrigger>
</e:Interaction.Triggers>
</toolkit:DataGrid>
The data source is as follow:
ObservableCollectionEx<GamehostDataModel> gameHostDataList = new ObservableCollectionEx<GamehostDataModel>();
After sorting the column on the grid by clicking on the column header, the entries look sorted but when I click on the first row, the data corresponding from the unsorted list shows up. I am just wondering what is the correlation between the visual representation of the itemsource and the actual itemsource data?
Let's say for example:
Data Visually Data Itemsource
2 2
3 3
1 1
After clicking on the header to sort we have
Data Visually Data Itemsource
1 2
2 3
3 1
Is it supposed to rearrange the ref开发者_StackOverflowerence data source too?
That sounds like a bug to me. The ItemsSource should NOT be sorted by the grid.
I don't know much about the Toolkit DataGrid, but if it's like anything else in WPF, it won't sort your underlying ItemsSource. (How could it? Your ItemsSource could be a read-only IEnumerable.) Instead, it will create a collection view that wraps your ItemsSource items, and the collection view is what gets sorted. Collection views are how WPF supports sorting and grouping.
If you want to get a reference to the collection view (which contains the sorted list), see "How to: Get the Default View of a Data Collection".
i assume that your real problem is to get the right selected item. so please do NOT use
<DataGrid SelectedIndex="{Binding CurrentSelectedIdx}"
<e:Interaction.Triggers>
<e:EventTrigger EventName ="SelectionChanged">
<b:CommandAction Command="{Binding DisplayGhCommand}"/>
</e:EventTrigger>
</e:Interaction.Triggers>
you should use the SelectedItem property to get what you want.
<DataGrid SelectedItem="{Binding MyViewModelSelectedItemProperty}" ...
NO it is not supposed to do that, It is visualizing your data just assumed that you have huge data source and visualize it in a datagrid and each time you sorting that in datagrid it goes to rearrange your data source! You can do if you want by hooking up a handler.
By the way you don’t need toolkit if you can use .NET 4.0 it has datagrid control as an embedded data element.
精彩评论