,我认为你是什么就可以实现后更容易
假设你有一个型号
public class Item
{
public bool IsDeleted { get; set; }
public string Name { get; set; }
}
而且你视图模型包含集合
public ObservableCollection<Item> MyItems { get; set; }
你可以添加ICollectionView
财产,将过滤您的收藏由你删除项目。这里有一个例子:
public ICollectionView UndeletedItems { get; set; }
筛选逻辑:
// Collection which will take your ObservableCollection
var itemSourceList = new CollectionViewSource { Source = MyItems };
// ICollectionView the View/UI part
UndeletedItems = itemSourceList.View;
//add the Filter
UndeletedItems.Filter = new Predicate<object>(item => !((Item)item).IsDeleted);
然后绑定你查看到UndeletedItems
代替
<DataGrid ItemsSource="{Binding UndeletedItems}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
这将隐藏已删除的项目,同时还支持CRUD操作。
希望这有助于
原谅我,如果语法有点关闭我从内存中写道。另外,我只是为了例子而忽视了'INotifyPropertyChanged'。 –
我更新了问题,指出该集合仍必须支持经典的CRUD操作。除非我错了(我是WPF的新手),在您的场景中,UndeletedItems将返回一个不允许网格添加或删除记录的枚举。 – Crono
@ Crono1981目前无法测试或玩弄它,但我会在明天进行测试,一旦我在Visual Studio之前,并且一致地更新我的答案。 –