如何使用 MVVM 在 Silverlight 中创建依赖属性?

发布于 2024-12-15 12:53:45 字数 86 浏览 4 评论 0原文

假设有一个文本框,我想使用 MVVM 控制该控件的可见性,是否有关于如何执行此操作的示例?首先创建一个依赖属性,然后将其连接到 ViewModel 中。谢谢。

let say there is a textbox and i want to control the visibility of this control using MVVM, is there a sample on how to do this? First create a dependency property then get it hooked up in the ViewModel. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

初见 2024-12-22 12:53:45

通常,在这种情况下您不需要使用依赖属性。依赖属性实际上只需要为控件本身之类的东西实现,而不是为了确定行为。行为(例如元素的可见性)可以直接通过数据绑定进行处理。

您的 ViewModel 将只有一些属性,并且您将绑定 TextBox。Visibility 属性直接添加到 ViewModel 属性。

一个“棘手的问题”是您经常需要某种类型的 IValueConverter 将从您的属性类型转换为 可见性枚举。

Typically, you wouldn't need to use a dependency property in this case. Dependency properties really only need to be implemented for things like controls themselves, not for determining behavior. Behavior, such as the visibility of an element, can be handled directly via data binding.

Your ViewModel would just have some property, and you'd bind the TextBox.Visibility property directly to the ViewModel property.

The one "sticky point" is that you often will want to have some type of IValueConverter that will convert from your property type to a Visibility enum.

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