【问题标题】:Reset DataGrid Sorting back to its initial state将 DataGrid 排序重置为其初始状态
【发布时间】:2016-07-10 19:30:42
【问题描述】:

我创建了一个包含一些按钮和一个 DataGrid 的视图。 DataGrid 在应用程序解析 excel 文件时显示项目。我添加了一个取消解析线程的按钮。我能够清除 DataGrid 我遇到的唯一问题是,如果任何列已经排序,那么排序在取消后仍然存在。

这是我的 Datagrid 的示例:

<DataGrid ItemsSource="{Binding Parts}"
                  Visibility="{Binding DatagridIsVisible, Converter={StaticResource BooleanToVisibilityInverseParameterConverter}}"
                  AutoGenerateColumns="False" 
                  Margin="0,0,0,44" 
                  FontSize="20" 
                  AlternationCount="2"
                  AlternatingRowBackground="WhiteSmoke"
                  CanUserAddRows="False">
  <DataGrid.Columns >
    <DataGridTextColumn  Header="Raw Data" 
                         Binding="{Binding RawData}">
        <DataGridTextColumn.CellStyle>
            <Style>
                <Setter Property="Border.BorderBrush" Value="Black"/>
                <Setter Property="Border.BorderThickness" Value="0 0 1 0"/>
            </Style>
        </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>   
    <DataGridTextColumn  Header="Manufacturer" Binding="{Binding Manu}">
        <DataGridTextColumn.CellStyle>
            <Style>
                <Setter Property="Border.BorderBrush" Value="Black"/>
                <Setter Property="Border.BorderThickness" Value="0 0 1 0"/>
            </Style>
        </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>       
  </DataGrid.Columns>
</DataGrid>

我已经通过互联网进行了梳理,并找到了有关如何使用代码隐藏方法执行此操作的示例,但我真的试图为此坚持使用 MVVM。

这可能吗?

【问题讨论】:

  • Parts (ItemsSource) 的类别是什么?
  • Parts 是我创建的仅包含字符串的类型的 ObservableCollection 的属性

标签: c# wpf mvvm datagrid


【解决方案1】:

我认为您应该为您的 ItemsSource 使用 Collection View 而不是 Observable Collection,因为如果您使用 Observable Collection,那么您只有一个 Collection View,因此所有更改都会保留。但是,如果您使用集合视图,则可以更改集合视图并删除排序。

你可以see more here

希望对你有帮助

【讨论】:

  • 嘿,感谢您的回答,我似乎已经清除了排序,但是显示排序是升序还是降序的箭头被留下了。
猜你喜欢
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
  • 2021-09-05
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
相关资源
最近更新 更多