我有一个绑定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>
答案 0 :(得分:14)
我会使用ICollectionView进行过滤。
public ICollectionView MyView {get; private set;}
所以你可以这样做:
MyView = CollectionViewSource.GetDefaultView(OrderTemplateList);
MyView.Filter = MyFilterAction;
所以当你的&#34; button_ClickCommand&#34;在您的Viewmodel中设置一个新状态,您只需调用
即可MyView.Refresh();
将重新评估您的过滤器
答案 1 :(得分:-5)
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将是您的命令处理程序