WPF:软删除和绑定?
我有实现 INotifyProperyChanged 的自定义对象,现在我想知道是否可以实现与绑定配合良好的软删除?每个对象都有一个 IsDeleted 属性,如果该属性设置为 true,则它不会在 GUI 中显示。我正在考虑制作一个自定义标记扩展来装饰 Binding 类,但它没有按预期工作。现在我正在考虑使用 MultiBinding 和 IsDeleted 作为绑定属性之一,以便转换器能够确定删除了哪个对象。但这个解决方案听起来相当复杂和无聊。
有人知道如何实现软删除绑定吗?
I have custom objects which implement INotifyProperyChanged and now I'm wondering if it is possible to implement soft delete which would play nicely with binding? Each object would have a IsDeleted property and if this property is set to true than it would not be displayed in GUI. I was thinking about making a custom markup extension which would decorate Binding class but it hadn't worked out as expected. Now I'm considering using MultiBinding with IsDeleted as one of bound properties so that converter would be able to figure out which object is deleted. But this solution sounds quite complicated and boring.
Does anybody have an idea how to implement soft deletes for binding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 UIElement 的属性可见性绑定到对象的 IsDeleted 属性,以隐藏或显示元素。
作为示例,我使用 TextBlock。在 XAML 中,您可以编写
注意:在上面的示例中,当 IsDeleted 为 true 时,TextBlock 是可见的。我会定义一个正属性,例如对象上的 Exists。因此,您不必否定布尔值或构建自己的转换器。
WPF 有一个内置转换器,可以将布尔值转换为 Visibility 的枚举值。
You can bind the Property Visibility of the UIElement to the property IsDeleted of your object, to hide or show the elements.
As an example i use a TextBlock. In XAML you can write
NOTE: In the example above, the TextBlock is visible, when IsDeleted is true. I would define a positive property, such as Exists on the object. So you do not have to negate the boolean or to build your own converter.
WPF has a buildin converter that converts boolean to an enum value of Visibility.
实现软删除的另一种方法是维护和公开一个集合,除了所有项目的集合之外,该集合仅包含视图模型中尚未删除的项目。 (在我看来)这有一个非常大的优点,那就是你的观点根本不需要考虑它。
Another way to implement soft deletes is by maintaining and exposing a collection containing only those items that haven't been deleted in your view model in addition to the collection of all items. This has the (to my mind) very great merit that it's not something your view needs to think about at all.