为什么将 INotifyPropertyChanged 与 WPF 中的绑定一起使用? [英] Why use INotifyPropertyChanged with bindings in WPF?

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

问题描述

我注意到,实际上我在 Internet 上找到的关于绑定的每个示例都有一个类(绑定到另一个属性),该类继承 INotifyPropertyChanged 接口并使用类属性的 set 部分中的方法.

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

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 类:

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

推荐答案

如果您只想使用绑定来写入到属性,则不需要 INotifyPropertyChanged(正如您所发现的),但您确实需要它,以便您可以告诉其他人写入该属性并相应地更新显示的值.

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.

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

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.

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

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