INotifyPropertyChanged WPF

发布于 2024-07-26 04:17:35 字数 869 浏览 6 评论 0原文

INotifyPropertyChanged 的​​目的是什么。 我知道每当属性更改时都会触发此事件,但 View/UI 如何知道此事件已触发:

这是我的 Customer 类,它实现了 INotifyPropertyChanged 事件:

public class Customer : INotifyPropertyChanged
    {
        private string _firstName;

        public string LastName { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if(PropertyChanged != null)
                PropertyChanged(this,new PropertyChangedEventArgs(propertyName));

        }

        public string FirstName
        {
            get { return _firstName; }

            set
            {
                _firstName = value;
                OnPropertyChanged("FirstName");
            }
        }
    }

但现在如何通知 UI 属性已更改。 就像当用户将 null 或空分配给名字时,如何在 UI 上显示 MessageBox。

What is the purpose of INotifyPropertyChanged. I know this event is fired whenever a property is changed but how can the View/UI knows that this event is fired:

Here is my Customer class that implements the INotifyPropertyChanged event:

public class Customer : INotifyPropertyChanged
    {
        private string _firstName;

        public string LastName { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if(PropertyChanged != null)
                PropertyChanged(this,new PropertyChangedEventArgs(propertyName));

        }

        public string FirstName
        {
            get { return _firstName; }

            set
            {
                _firstName = value;
                OnPropertyChanged("FirstName");
            }
        }
    }

But now how to notify the UI that property has changed. Like when the user assigns null or empty to the first name how can I display a MessageBox on the UI.

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

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

发布评论

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

评论(4

宁愿没拥抱 2024-08-02 04:17:35

INotifyPropertyChanged 允许 WPF UI 元素(通过标准数据绑定机制)订阅 PropertyChanged 事件并自动更新自身。 例如,如果您有一个显示 FirstName 属性的 TextBlock,则通过使用 INotifyPropertyChanged,您可以将其显示在表单上,​​并且当 FirstName 属性在代码中更改时,它将自动保持最新状态。

视图只是订阅事件——它告诉它所有必要的事情。 该事件包含已更改属性的名称,因此如果 UI 元素绑定到该属性,它就会更新。

INotifyPropertyChanged allows WPF UI elements (via the standard data binding mechanisms) to subscribe to the PropertyChanged event and automatically update themselves. For example, if you had a TextBlock displaying your FirstName property, by using INotifyPropertyChanged, you can display it on a form, and it will automatically stay up to date when the FirstName property is changed in code.

The View just subscribes to the event - which tells it everything necessary. The event includes the name of the changed property, so if a UI element is bound to that property, it updates.

寄人书 2024-08-02 04:17:35

WPF 可以知道,因为它可以检查对象是否实现此接口,然后将对象强制转换为所述接口并注册事件。 然后它可以触发绑定基础设施来更新显示。 如果您也想做出反应,可以注册参加同一活动。

WPF can know because it can inspect whether an object implements this interface, then cast the object to said interface and register for the event. It can then trigger the Binding Infrastructure to update the display. If you want to react as well, you can register yourself for the same event.

抽个烟儿 2024-08-02 04:17:35

编辑:我重读了你的问题和你的一些评论。 这是利用 DataContextChanged 的可能解决方案事件和 Customer 对象上的 INotifyPropertyChanged 接口。 您还应该查看 数据WPF 和 .Net 3.5 中的绑定验证

<TextBox Text="{Binding FirstName}" />

// assuming:
// myWindow.DataContext = new Customer();
myWindow.DataContextChanged += MyWindow_DataContextChanged;

private void MyWindow_DataContextChanged(object sender,
    DependencyPropertyChangedEventArgs e)
{
    var oldCustomer = e.OldValue as Customer;
    if (oldCustomer != null)
    {
        oldCustomer.PropertyChanged -= Customer_CheckProps;
    }

    var newCustomer = e.NewValue as Customer;
    if (newCustomer != null)
    {
        newCustomer.PropertyChanged += Customer_CheckProps;
    }
}

private void Customer_CheckProps(object sender, PropertyChangedEventArgs e)
{
    var customer = sender as Customer;
    if (customer != null)
    {
        if (e.PropertyName == "FirstName"
            && String.IsNullOrEmpty(customer.FirstName))
        {
            // Display Message Box
        }
    }
}

EDIT: I reread your question and some of your comments. Here is a possible solution utilizing the DataContextChanged event and the INotifyPropertyChanged interface on your Customer object. You should also look into Data Binding Validation in WPF and .Net 3.5.

<TextBox Text="{Binding FirstName}" />

// assuming:
// myWindow.DataContext = new Customer();
myWindow.DataContextChanged += MyWindow_DataContextChanged;

private void MyWindow_DataContextChanged(object sender,
    DependencyPropertyChangedEventArgs e)
{
    var oldCustomer = e.OldValue as Customer;
    if (oldCustomer != null)
    {
        oldCustomer.PropertyChanged -= Customer_CheckProps;
    }

    var newCustomer = e.NewValue as Customer;
    if (newCustomer != null)
    {
        newCustomer.PropertyChanged += Customer_CheckProps;
    }
}

private void Customer_CheckProps(object sender, PropertyChangedEventArgs e)
{
    var customer = sender as Customer;
    if (customer != null)
    {
        if (e.PropertyName == "FirstName"
            && String.IsNullOrEmpty(customer.FirstName))
        {
            // Display Message Box
        }
    }
}
昔梦 2024-08-02 04:17:35

INotifyPropertyChabged 对象允许您通知观察者该对象的属性已更改,并使用 Subscribe(...) 方法采取相应的操作。

摘自 https://github.com/sergio235/Venn 文档:

var observable = myObject.WhenAny(obj => obj.MyProperty);
observable.Subscribe(newValue => {
     // Handle property change
});

An INotifyPropertyChabged object allows you to notify observers that a property of that object has changed and act accordingly using the Subscribe(...) method.

Extracted from https://github.com/sergio235/Venn documentation:

var observable = myObject.WhenAny(obj => obj.MyProperty);
observable.Subscribe(newValue => {
     // Handle property change
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文