简体   繁体   English

将 DataGrid 排序重置为其初始状态

[英]Reset DataGrid Sorting back to its initial state

I have created a view that contains some buttons and a DataGrid.我创建了一个包含一些按钮和一个 DataGrid 的视图。 The DataGrid displays items as the application parses an excel file. DataGrid 在应用程序解析 Excel 文件时显示项目。 I have added a button that cancels the parsing thread.我添加了一个取消解析线程的按钮。 I am able to clear the DataGrid the only issue I am having is if any of the columns have been sorted that sort persists through the cancel.我能够清除 DataGrid 我遇到的唯一问题是,如果任何列已经排序,排序在取消时仍然存在。

Heres an example of my 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>

I have combed through the internet and have found examples on how to do this using the code behind method but I am really trying to stick to MVVM for this.我已经通过互联网梳理并找到了有关如何使用代码隐藏方法执行此操作的示例,但我真的试图为此坚持使用 MVVM。

Is this even possible?这甚至可能吗?

I think that you should use a Collection View instead of an Observable Collection for your ItemsSource , because if you use an Observable Collection you only have a Collection View so all changes are kept.我认为你应该为你的 ItemsSource使用Collection View 而不是 Observable Collection ,因为如果你使用 Observable Collection 你只有一个 Collection View 所以所有更改都会保留。 However if you use a Collection View you can change the collection view and remove the sorting.但是,如果您使用集合视图,则可以更改集合视图并删除排序。

You can see more here你可以在这里看到更多

I hope that this can help you我希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM