WPF:软删除和绑定?

发布于 2024-08-26 07:26:07 字数 260 浏览 15 评论 0原文

我有实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一直在等你来 2024-09-02 07:26:07

您可以将 UIElement 的属性可见性绑定到对象的 IsDeleted 属性,以隐藏或显示元素。

作为示例,我使用 TextBlock。在 XAML 中,您可以编写

<TextBlock Text="IsDeleted" Visibility={Binding IsDeleted}/>

注意:在上面的示例中,当 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

<TextBlock Text="IsDeleted" Visibility={Binding IsDeleted}/>

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.

把回忆走一遍 2024-09-02 07:26:07

实现软删除的另一种方法是维护和公开一个集合,除了所有项目的集合之外,该集合仅包含视图模型中尚未删除的项目。 (在我看来)这有一个非常大的优点,那就是你的观点根本不需要考虑它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文