Datagrid列排序问题,排序在视觉上看起来不错,但ItemSource仍然相同

发布于 2024-12-06 18:41:53 字数 3495 浏览 0 评论 0原文

这是我定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

刘备忘录 2024-12-13 18:41:53

这对我来说听起来像是一个错误。 ItemsSource 不应按网格排序。

That sounds like a bug to me. The ItemsSource should NOT be sorted by the grid.

ぺ禁宫浮华殁 2024-12-13 18:41:53

我对 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".

童话 2024-12-13 18:41:53

我认为您真正的问题是获得正确的选择项目。所以请不要使用

 <DataGrid SelectedIndex="{Binding CurrentSelectedIdx}" 
      <e:Interaction.Triggers>
           <e:EventTrigger EventName ="SelectionChanged">
               <b:CommandAction Command="{Binding DisplayGhCommand}"/>
           </e:EventTrigger>
      </e:Interaction.Triggers>

您应该使用 SelectedItem 属性来获取您想要的内容。

 <DataGrid SelectedItem="{Binding MyViewModelSelectedItemProperty}" ...

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}" ...
独孤求败 2024-12-13 18:41:53

不,它不应该这样做,它正在可视化您的数据,只是假设您有巨大的数据源并将其可视化在数据网格中,每次您在数据网格中对其进行排序时,它都会重新排列您的数据源!如果您愿意,您可以通过连接处理程序来完成。
顺便说一句,如果您可以使用 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文