为什么要在WPF绑定使用INotifyPropertyChanged的? [英] Why use INotifyPropertyChanged with bindings in WPF?

查看:108
本文介绍了为什么要在WPF绑定使用INotifyPropertyChanged的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经notived,几乎每一个例子中,我找到绑定在互联网上有继承了INotifyPropertyChanged接口,并使用了类的属性集组成部分的方法的类(绑定到另一个属性)。

我试过去除部分从绑定的例子,它的工作一样,因为它会与方法。

下面的例子。我已经改变了它,所以它会是一个双向bindingmode并显示一个消息框更改的属性。

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

修改

XAML:

 <窗​​口x:类=WpfApplication1.MainWindow
        的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
        标题=主窗口HEIGHT =350WIDTH =525>
    <网格和GT;
        < Grid.RowDefinitions>
            &所述; RowDefinition高度=30/>
            &所述; RowDefinition高度=30/>
            &所述; RowDefinition高度=30/>
            &所述; RowDefinition高度=30/>
            &所述; RowDefinition高度=30/>
            < RowDefinition高度=40/>
            &所述; RowDefinition高度=30/>
            &所述; RowDefinition高度=30/>
            &所述; RowDefinition高度=30/>
            &所述; RowDefinition高度=30/>
        < /Grid.RowDefinitions>
        < Grid.ColumnDefinitions>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            < ColumnDefinition WIDTH =100/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
            &所述; ColumnDefinition宽度=30/>
        < /Grid.ColumnDefinitions>
        <按钮Grid.Row =5Grid.Column =5NAME =btnBinding点击=btnBinding_ClickWIDTH =100HEIGHT =30>
            <电网的Horizo​​ntalAlignment =左VerticalAlignment =中心>
                < Grid.RowDefinitions>
                    < RowDefinition高度=25/>
                < /Grid.RowDefinitions>
                < Grid.ColumnDefinitions>
                    < ColumnDefinition宽度=50/>
                    < ColumnDefinition宽度=50/>
                < /Grid.ColumnDefinitions>
                <文本框名称=txtBindingWIDTH =30HEIGHT =25的Horizo​​ntalAlignment =左/>
                <标签Grid.Column =1CONTENT =绑定/>
            < /网格和GT;
        < /按钮>
        <按钮Grid.Column =5Grid.Row =6NAME =btnMessage点击=btnMessage_ClickCONTENT =MessageBox的/>
        <按钮Grid.Column =5Grid.Row =4NAME =btnChangeproperty点击=btnChangeproperty_ClickCONTENT =更改属性/>
    < /网格和GT;
< /窗GT;

Main.cs:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Windows;
使用System.Windows.Controls的;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;命名空间WpfApplication1
{
    ///<总结>
    ///为MainWindow.xaml交互逻辑
    ///< /总结>
    公共部分类主窗口:窗口
    {
        绑定绑定;
        迈德特MYDATA;
        公共主窗口()
        {
            的InitializeComponent();
        }        私人无效btnBinding_Click(对象发件人,RoutedEventArgs E)
        {
            MYDATA =新迈德特(T);
            绑定=新的绑定(MyDataProperty)
            {
                来源= MYDATA,
                模式= BindingMode.TwoWay
            };            txtBinding.SetBinding(TextBox.TextProperty,绑定);
        }        私人无效btnMessage_Click(对象发件人,RoutedEventArgs E)
        {
            MessageBox.Show(mydata.MyDataProperty);
        }        私人无效btnChangeproperty_Click(对象发件人,RoutedEventArgs E)
        {
            mydata.MyDataProperty =新的绑定;
        }
    }
}

MyData的类:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.ComponentModel;命名空间WpfApplication1
{
    公共类迈德特
    {
        私人字符串myDataProperty;        公共迈德特(){}        公共迈德特(日期时间DATETIME)
        {
            myDataProperty =上站时间是+ dateTime.ToLongTimeString();
        }        公共迈德特(字符串睾丸)
        {
            myDataProperty =睾丸;
        }        公共字符串MyDataProperty
        {
            {返回myDataProperty; }
            组
            {
                myDataProperty =价值;
                OnPropertyChanged(MyDataProperty);
            }
        }        公共事件PropertyChangedEventHandler的PropertyChanged;        私人无效OnPropertyChanged(字符串信息)
        {
            PropertyChangedEventHandler处理器=的PropertyChanged;
            如果(处理!= NULL)
            {
                处理器(这一点,新PropertyChangedEventArgs(信息));
            }
        }
    }
}


解决方案

您不需要 INotifyPropertyChanged的如果你只打算使用绑定的的的财产(如你发现了),但你确实需要它,这样你可以知道的他人的写的财产,并相应地更新显示的值。

要明白我在说一下,添加一个按钮,你的窗口,点击后直接改变绑定属性的值(的绑定到该属性的UI元素的相应属性) 。随着 INotifyPropertyChanged的,你会看到UI更新自己新的价值,当你点击按钮;没有它,用户界面​​仍然会显示旧的价值。

I've notived that practically every example I find on the internet about bindings has a class (which binds to another property) that inherits the INotifyPropertyChanged interface and uses a method in the set part of the class' property.

I've tried removing that part from the binding example and it worked the same as it would with the method.

Here's the example. I've altered it so it would be a TwoWay bindingmode and show the changed property in a messagebox.

I did that just to play around a little bit with bindings, but now I really don't know why that interface is used

EDIT

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="30"/>
        </Grid.ColumnDefinitions>
        <Button Grid.Row="5" Grid.Column="5" Name="btnBinding" Click="btnBinding_Click" Width="100" Height="30">
            <Grid HorizontalAlignment="Left" VerticalAlignment="Center">
                <Grid.RowDefinitions>
                    <RowDefinition Height="25"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="50"/>
                </Grid.ColumnDefinitions>
                <TextBox Name="txtBinding" Width="30" Height="25" HorizontalAlignment="Left"/> 
                <Label Grid.Column="1" Content="Bind"/>
            </Grid>
        </Button>
        <Button Grid.Column="5" Grid.Row="6" Name="btnMessage" Click="btnMessage_Click" Content="MessageBox"/>
        <Button Grid.Column="5" Grid.Row="4" Name="btnChangeproperty" Click="btnChangeproperty_Click" Content="Change Property"/>
    </Grid>
</Window>

Main.cs:

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";
        }
    }
}

MyData class:

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));
            }
        }
    }
}

解决方案

You don't need INotifyPropertyChanged if you only intend to use the binding to write to the property (as you have found out), but you do need it so that you can tell that someone else wrote to the property and update the displayed value accordingly.

To see what I 'm talking about, add a button to your window that when clicked directly changes the value of the bound property (not the corresponding attribute of the UI element bound to that property). With INotifyPropertyChanged, you will see the UI updating itself to the new value when you click the button; without it, the UI will still show the "old" value.

这篇关于为什么要在WPF绑定使用INotifyPropertyChanged的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆