为什么 OnPropertyChanged 在代码隐藏中不起作用?

发布于 2024-08-09 12:21:25 字数 2057 浏览 4 评论 0原文

我试图通过将 ViewModel 模型放入后面的代码并将 DataContext 绑定为“this”来简化一些代码,但在以下示例中,它的工作方式似乎有所不同:

为什么单击按钮时,即使调用 OnPropertyChanged("Message"),绑定到“Message”的 TextBlock 也不会更改?

XAML:

<Window x:Class="TestSimple223.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left">
        <Button Content="Button" 
                Click="button1_Click" />
        <TextBlock 
            Text="{Binding Path=Message, Mode=TwoWay}"/>
        <TextBlock
            x:Name="Message2"/>
    </StackPanel>
</Window>

代码隐藏:

using System.Windows;
using System.ComponentModel;

namespace TestSimple223
{
    public partial class Window1 : Window
    {
        #region ViewModelProperty: Message
        private string _message;
        public string Message
        {
            get
            {
                return _message;
            }

            set
            {
                _message = value;
                OnPropertyChanged("Message");
            }
        }
        #endregion

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            Message = "original message";
            Message2.Text = "original message2";
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Message = "button was clicked, message changed";
            Message2.Text = "button was click, message2 changed";
        }

        #region INotify
        public event PropertyChangedEventHandler PropertyChanged;

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


    }
}

I'm trying to simplify some code by putting the ViewModel models into the code behind and binding the DataContext as "this", but it seems to work differently, in the following example:

Why is it when the button is clicked, the TextBlock bound to "Message" does not change, even though OnPropertyChanged("Message") is called?

XAML:

<Window x:Class="TestSimple223.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left">
        <Button Content="Button" 
                Click="button1_Click" />
        <TextBlock 
            Text="{Binding Path=Message, Mode=TwoWay}"/>
        <TextBlock
            x:Name="Message2"/>
    </StackPanel>
</Window>

Code Behind:

using System.Windows;
using System.ComponentModel;

namespace TestSimple223
{
    public partial class Window1 : Window
    {
        #region ViewModelProperty: Message
        private string _message;
        public string Message
        {
            get
            {
                return _message;
            }

            set
            {
                _message = value;
                OnPropertyChanged("Message");
            }
        }
        #endregion

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            Message = "original message";
            Message2.Text = "original message2";
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Message = "button was clicked, message changed";
            Message2.Text = "button was click, message2 changed";
        }

        #region INotify
        public event PropertyChangedEventHandler PropertyChanged;

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


    }
}

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

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

发布评论

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

评论(1

紫轩蝶泪 2024-08-16 12:21:25

您尚未将您的班级标记为可用于属性更改通知。将标题更改为

public partial class Window1 : Window, INotifyPropertyChanged

“仅仅因为您实现了这些方法”并不意味着 WPF 知道某个类支持更改通知 - 您需要通过用 INotifyPropertyChanged 标记它来告诉它。这样,绑定机制就可以将您的类识别为潜在的更新目标。

You haven't marked your class as being available for property change notification. Change the heading to

public partial class Window1 : Window, INotifyPropertyChanged

Just because you implement the methods doesn't mean that WPF knows that a class supports change notification - you need to tell it by marking it with INotifyPropertyChanged. This way, the binding mechanism can identify your class as a potential update target.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文