在 WPF MVVM 中过滤 ObservableCollection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28233906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filtering ObservableCollection in WPF MVVM
提问by Samudra Lakmal
I have a data-grid which is bind ObservableCollection. There is an Status field. I need to Filter by status when click on filter button. I use MVVM and need to filter inside the VM class.
我有一个绑定 ObservableCollection 的数据网格。有一个状态字段。单击过滤器按钮时,我需要按状态过滤。我使用 MVVM 并且需要在 VM 类中进行过滤。
<DataGrid
CanUserResizeColumns="False" CanUserResizeRows="False" IsEnabled="{Binding IsKeySet}"
Name="dgwTemplateDetails" CanUserAddRows="False" SelectionMode="Single"
ItemsSource="{Binding OrderTemplateList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" Width="auto" FontWeight="Normal" FontStyle="Normal">
<DataGrid.Columns>
<DataGridTextColumn Header="Act Code" Width="75" Binding="{Binding ActCode}" IsReadOnly="True"/>
<DataGridTextColumn Header="Act Name" Width="*" Binding="{Binding ActName}" IsReadOnly="True"/>
<DataGridTextColumn Header="No. Of Days" Width="80" Binding="{Binding NoOfDays}"/>
<DataGridTemplateColumn Header="To be Finish Date" Width="110" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding EarStartDt, StringFormat=dd/MM/yyyy, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding EarStartDt, StringFormat=dd/MM/yyyy, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Act Status" Width="100" Binding="{Binding ActStatus}" IsReadOnly="True"/>
<DataGridTextColumn Header="Remarks" Width="200" Binding="{Binding Remarks}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
回答by blindmeis
I would use the ICollectionView for Filtering.
我会使用 ICollectionView 进行过滤。
public ICollectionView MyView {get; private set;}
so in your ctor you can do:
所以在你的 ctor 你可以这样做:
MyView = CollectionViewSource.GetDefaultView(OrderTemplateList);
MyView.Filter = MyFilterAction;
so when your "button_ClickCommand" set an new Status in your Viewmodel you can simply call
所以当你的“button_ClickCommand”在你的视图模型中设置一个新的状态时,你可以简单地调用
MyView.Refresh();
and your filter will be re-evaluated
并且您的过滤器将被重新评估
回答by Muds
ObservableCollection<object> orderTemplateList = new ObservableCollection<object>();
private bool toFilter;
ObservableCollection<object> OrderTemplateList
{
get
{
if (toFilter)
return orderTemplateList.Where(c => c.Status = FilterStatus);
else
return orderTemplateList;
}
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
toFilter = !toFilter;
OnPropertyChanged("OrderTemplateList");
}
ButtonClick will be your command handler here
ButtonClick 将成为您的命令处理程序

