Datagrid列排序问题,排序在视觉上看起来不错,但ItemSource仍然相同
这是我定义 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>
数据源如下:
ObservableCollectionEx<GamehostDataModel> gameHostDataList = new ObservableCollectionEx<GamehostDataModel>();
通过单击列标题对网格上的列进行排序后,条目看起来已排序,但是当我单击第一行时,会显示未排序列表中对应的数据。我只是想知道 itemsource 的视觉表示和实际 itemsource 数据之间的相关性是什么?
举例来说:
Data Visually Data Itemsource
2 2
3 3
1 1
单击标题进行排序后,我们
Data Visually Data Itemsource
1 2
2 3
3 1
是否也应该重新排列参考数据源?
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 reference data source too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我来说听起来像是一个错误。 ItemsSource 不应按网格排序。
That sounds like a bug to me. The ItemsSource should NOT be sorted by the grid.
我对 Toolkit DataGrid 了解不多,但如果它像 WPF 中的其他任何东西一样,它不会对您的基础 ItemsSource 进行排序。 (怎么可能呢?您的 ItemsSource 可能是只读的 IEnumerable。)相反,它将创建一个包装您的 ItemsSource 项的集合视图,并且集合视图将被排序。集合视图是 WPF 支持排序和分组的方式。
如果要获取对集合视图(其中包含排序列表)的引用,请参阅“
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".
我认为您真正的问题是获得正确的选择项目。所以请不要使用
您应该使用 SelectedItem 属性来获取您想要的内容。
i assume that your real problem is to get the right selected item. so please do NOT use
you should use the SelectedItem property to get what you want.
不,它不应该这样做,它正在可视化您的数据,只是假设您有巨大的数据源并将其可视化在数据网格中,每次您在数据网格中对其进行排序时,它都会重新排列您的数据源!如果您愿意,您可以通过连接处理程序来完成。
顺便说一句,如果您可以使用 .NET 4.0,则不需要工具包,它具有数据网格控件作为嵌入式数据元素。
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.