C# 为什么在WPF中使用带绑定的INotifyPropertyChanged?

C# 为什么在WPF中使用带绑定的INotifyPropertyChanged?,c#,wpf,binding,C#,Wpf,Binding,我发现,实际上我在internet上找到的关于绑定的每个示例都有一个类(绑定到另一个属性),该类继承INotifyPropertyChanged接口,并在类的属性的集合部分使用方法 我尝试过从绑定示例中删除该部分,其工作原理与使用该方法时相同 下面是一个例子。我修改了它,使其成为双向绑定模式,并在messagebox中显示更改的属性 我这样做只是为了玩一点绑定,但现在我真的不知道为什么要使用这个接口 XAML: 如果您只想使用绑定写入属性(如您所发现的),则不需要INotifyPropertyC

我发现,实际上我在internet上找到的关于绑定的每个示例都有一个类(绑定到另一个属性),该类继承INotifyPropertyChanged接口,并在类的属性的集合部分使用方法

我尝试过从绑定示例中删除该部分,其工作原理与使用该方法时相同

下面是一个例子。我修改了它,使其成为双向绑定模式,并在messagebox中显示更改的属性

我这样做只是为了玩一点绑定,但现在我真的不知道为什么要使用这个接口

XAML:


如果您只想使用绑定写入属性(如您所发现的),则不需要
INotifyPropertyChanged
,但您确实需要它,这样您就可以知道其他人写入了属性,并相应地更新显示的值


要了解我在说什么,请在窗口中添加一个按钮,当直接单击该按钮时,该按钮会更改绑定属性的值(而不是绑定到该属性的UI元素的相应属性)。使用
INotifyPropertyChanged
,单击按钮时,您将看到UI自身更新为新值;如果没有它,UI仍然会显示“旧”值。

从这里的讨论中,我认为您错过了实现

RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(“Propety Name”))
一旦实现,您可以看到UI正在自动更新。
您可以在MSDN上查看详细信息,也可以在

查看简要版本。非常感谢你的回答。它真的帮助了我。我会在5分钟内把它标为正确答案。我需要这5分钟,因为stackoverflow不允许我将问题标记为answered@morcillo:很乐意帮忙。干杯好的,我不能将它标记为已回答,因为我这样做了,但它不起作用。我取消了Inotify部分的注释(我不知道这个词是否正确..英语不是我的第一语言),它本应该有效,但没有。如果你愿意,我可以把我做的东西寄给你。这真的很简单。显示绑定值的文本框、更改绑定值的按钮以及在消息框中显示绑定值的按钮。messagebox工作正常,但textbox不工作updating@morcillo:编辑问题并在后面发布XAML和相关代码。我现在不能回答,因为该睡觉了,但我明天再看。@morcillo:一眼就看不出有什么不对。如果单击
btnChange
仅文本框不应更新,则不使用INPC;使用INPC,它应该更新为“新绑定”。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Binding bind;
        MyData mydata;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnBinding_Click(object sender, RoutedEventArgs e)
        {
            mydata = new MyData("T");
            bind = new Binding("MyDataProperty")
            {
                Source = mydata,
                Mode = BindingMode.TwoWay
            };

            txtBinding.SetBinding(TextBox.TextProperty, bind);
        }

        private void btnMessage_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(mydata.MyDataProperty);
        }

        private void btnChangeproperty_Click(object sender, RoutedEventArgs e)
        {
            mydata.MyDataProperty = "New Binding";
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WpfApplication1
{
    public class MyData 
    {
        private string myDataProperty;

        public MyData() { }

        public MyData(DateTime dateTime)
        {
            myDataProperty = "Last bound time was " + dateTime.ToLongTimeString();
        }

        public MyData(string teste)
        {
            myDataProperty = teste;
        }

        public String MyDataProperty
        {
            get { return myDataProperty; }
            set
            {
                myDataProperty = value;
                OnPropertyChanged("MyDataProperty");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string info)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(“Propety Name”))