INotifyPropertyChanged 和 DependencyProperty 之间有什么关系?

发布于 2024-07-21 06:28:47 字数 1969 浏览 6 评论 0原文

我正在使用 DependencyProperties 构建一个简单的 UserControl 示例,以便可以在 XAML 中更改控件的属性(代码如下)。

但当然,在我的应用程序中,我不希望此控件具有紧密耦合的代码隐藏,而是用户控件将是一个名为“DataTypeWholeNumberView”的视图,并且它将有自己的名为“DataTypeWholeNumberViewModel”的 ViewModel。

因此,我将在 ViewModel 中实现下面的 DependencyProperty 逻辑,但在 ViewModel 中,我通常继承 INotifyPropertyChanged,这似乎给了我相同的功能。

那么两者之间的关系是:

  1. 将 UserControl XAML 的 DataContext 绑定到其代码隐藏,该代码具有 DependencyProperties
  2. 将 UserControl XAML(视图)的 DataContext 绑定到其 ViewModel (继承自 INotifyPropertyChanged)并具有实现 INotifyPropertyChanged 功能的属性?

XAML:

<UserControl x:Class="TestDependencyProperty827.SmartForm.DataTypeWholeNumber"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal">
            <TextBlock Text="{Binding Label}"/>
        </StackPanel>
    </StackPanel>
</UserControl>

代码隐藏:

using System.Windows;
using System.Windows.Controls;

namespace TestDependencyProperty827.SmartForm
{
    public partial class DataTypeWholeNumber : UserControl
    {
        public DataTypeWholeNumber()
        {
            InitializeComponent();
            DataContext = this;
        }

        public string Label
        {
            get
            {
                return (string)GetValue(LabelProperty);
            }
            set
            {
                SetValue(LabelProperty, value);
            }
        }

        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(DataTypeWholeNumber),
            new FrameworkPropertyMetadata());
    }
}

I'm building a simple UserControl example with DependencyProperties so that the properties of the control can be changed in XAML (code below).

But of course in my application I don't want this control to have tightly-coupled code-behind, but instead the user control will be a view called "DataTypeWholeNumberView" and it will have its own ViewModel called "DataTypeWholeNumberViewModel".

So I am going to implement the DependencyProperty logic below into the ViewModel, but in ViewModels I usually inherit INotifyPropertyChanged which seems to give me the same functionality.

So what is the relationship between:

  1. binding the DataContext of UserControl XAML to its code behind which has a DependencyProperties
  2. binding the DataContext of UserControl XAML (View) to its ViewModel (which inherits from INotifyPropertyChanged) and has properties which implements INotifyPropertyChanged functionality?

XAML:

<UserControl x:Class="TestDependencyProperty827.SmartForm.DataTypeWholeNumber"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal">
            <TextBlock Text="{Binding Label}"/>
        </StackPanel>
    </StackPanel>
</UserControl>

Code Behind:

using System.Windows;
using System.Windows.Controls;

namespace TestDependencyProperty827.SmartForm
{
    public partial class DataTypeWholeNumber : UserControl
    {
        public DataTypeWholeNumber()
        {
            InitializeComponent();
            DataContext = this;
        }

        public string Label
        {
            get
            {
                return (string)GetValue(LabelProperty);
            }
            set
            {
                SetValue(LabelProperty, value);
            }
        }

        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(DataTypeWholeNumber),
            new FrameworkPropertyMetadata());
    }
}

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

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

发布评论

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

评论(3

谎言月老 2024-07-28 06:28:47

INotifyPropertyChanged 是 .Net 自 2.0 起就存在的接口。 它基本上允许对象在属性发生更改时发出通知。 引发此事件时,相关方可以执行某些操作。 它的问题是它只发布属性的名称。 因此,您最终会使用反射或一些不确定的 if 语句来确定处理程序中要做什么。

DependencyProperties 是一个更复杂的构造,它支持默认值,以更节省内存和性能的方式更改通知。

唯一的关系是 WPF 绑定模型支持通过 INotifyPropertyChanged 实现绑定到 DependencyProperties 或标准 Clr 属性。 您的 ViewModel 也可以是 DependecyObject,第三个选项是绑定到 ViewModel 的 DependencyProperties!

Kent Boogaart 写了一篇关于 ViewModel 的非常有趣的文章是 POCO 与 DependencyObject。

INotifyPropertyChanged is an interface that exists in .Net since 2.0. It basically allows objects to notify when a property has changed. An interested party can perform certain actions when this event is raised. The problem with it is that it only publishes the name of the property. So you end up using reflection or some iffy if statements to figure out what to do in the handler.

DependencyProperties are a more elaborate construct that supports default values, change notifications in a more memory-efficient and performant way.

The only relationship is that the WPF binding model supports binding to either DependencyProperties or to standard Clr properties, with an INotifyPropertyChanged implementation. Your ViewModel could be a DependecyObject as well and the third option would be to bind to the ViewModel's DependencyProperties!

Kent Boogaart wrote a very interesting article on having a ViewModel be a POCO vs a DependencyObject.

樱娆 2024-07-28 06:28:47

我真的不认为 DependencyProperties 和 INotifyPropertyChanged 之间存在关系。 这里唯一的魔力是 Binding 类/utils 足够智能,可以识别 DependencyProperty 并直接绑定到它,或者订阅绑定目标的 notification-property-changed 事件并等待该事件触发。

I don't really think there is a relationship between DependencyProperties and INotifyPropertyChanged. The only magic here is that the Binding classes/utils are smart enough to recognize a DependencyProperty and bind directly to it, or subscribe to the binding-target's notify-property-changed event and wait for that to fire.

哭泣的笑容 2024-07-28 06:28:47

使用 WPF,您可以绑定到 DependencyProperties 或实现 INotifyPropertyChanged 的​​ Properties。 这是一个选择的问题。

因此,您的问题分为将它们放在代码后面或视图模型中。 既然您已经提到您不希望后面有紧密耦合的代码,那么您最好拥有遵循 MVVM 模式的视图模型。

即使在视图模型中,您也可以使用 DependencyProperties,就像在后面的代码中所做的那样。

With WPF, you can either bind to DependencyProperties or to Properties which implement INotifyPropertyChanged. It's a matter of choice.

So your question breaks into either to have them in code behind or in view model. Since you have mentioned that you do not want a tightly-coupled code behind, you are better off having a view model following the MVVM pattern.

You can use the DependencyProperties even in your view model just like you have done in your code behind.

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