【问题标题】:Why use INotifyPropertyChanged with bindings in WPF?为什么在 WPF 中将 INotifyPropertyChanged 与绑定一起使用?
【发布时间】:2012-05-15 13:12:51
【问题描述】:

我注意到几乎我在互联网上找到的每个关于绑定的示例都有一个类(它绑定到另一个属性),它继承了 INotifyPropertyChanged 接口并在类属性的 set 部分中使用了一个方法。

我尝试从绑定示例中删除该部分,它的工作方式与使用该方法时相同。

这是一个例子。我已经对其进行了更改,使其成为双向绑定模式,并在消息框中显示更改后的属性。

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

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

【问题讨论】:

    标签: c# wpf binding


    【解决方案1】:

    如果您只打算使用绑定来写入到属性(如您所见),则不需要INotifyPropertyChanged,但您确实需要它,以便您知道其他人写入属性并相应地更新显示的值。

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

    【讨论】:

    • 我明白了。非常感谢您的回答。它真的帮助了我。我会在 5 分钟内将其标记为正确答案。我需要这 5 分钟,因为 stackoverflow 不允许我将问题标记为已回答
    • @morcillo:很高兴为您提供帮助。干杯!
    • 好的,我无法将其标记为已回答,因为我这样做了,但它没有用。我没有注释(我不知道这个词是否正确......英语不是我的第一语言)Inotify 部分,它应该有效,但它没有。如果你愿意,我可以把我做的东西发给你。这真的很简单。显示绑定值的文本框、更改绑定值的按钮和在消息框中显示绑定值的按钮。消息框工作正常,但文本框没有更新
    • @morcillo:编辑问题并发布您的 XAML 和相关代码隐藏。我现在无法回答,因为是时候打麻袋了,但我明天会看看。
    • @morcillo:第一眼看不出有什么问题。如果您单击btnChange,则没有 INPC,只有文本框不应该更新;对于 INPC,它应该更新为“新绑定”。
    【解决方案2】:

    从这里的讨论来看,我认为你错过了实施

    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(“Propety Name”))
    

    实施后,您可以看到 UI 正在自动更新。 您可以在 MSDN 上查看详细信息或在 my blog here. 上查看简要版本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 2011-01-16
      相关资源
      最近更新 更多