INotifyPropertyChanged.PropertyChanged 始终为 NULL

发布于 2024-11-03 00:27:06 字数 1756 浏览 0 评论 0原文

我知道我在这里做错了什么,但是什么。请大家看一下并指出我的错误。

单击按钮后,我将在文本框中看到“Peter”,但看不到“Jack”。

我的类

namespace App
{
    class Person : INotifyPropertyChanged
    {
        private string name;
        public String Name
        {
            get { return name; }
            set { name = value; OnPropertyChanged("Name"); }
        }
    public Person()
    {
        Name = "Peter";
    }

    public void SetName(string newname)
    {
        Name = newname;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }
}

}

我的 XAML

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:app="clr-namespace:App"
    Title="MainWindow" Height="400" Width="400">
<Grid>
    <Grid.Resources>
        <app:Person x:Key="person"/>
    </Grid.Resources>
    <TextBox  Width="100" Height="26" Text="{Binding Source={StaticResource person}, Path=Name, Mode=TwoWay}" />
    <Button Content="Button" Height="23"  Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>

和我的代码隐藏

public partial class MainWindow : Window
{
    Person person;

    public MainWindow()
    {
        InitializeComponent();

        person = new Person();       
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        person.SetName("Jack");
    }
}

谢谢。

I know I am doing something wrong here but what. Please have a look and point out my error.

I will see "Peter" in my textbox but no "Jack" after the button click.

My class

namespace App
{
    class Person : INotifyPropertyChanged
    {
        private string name;
        public String Name
        {
            get { return name; }
            set { name = value; OnPropertyChanged("Name"); }
        }
    public Person()
    {
        Name = "Peter";
    }

    public void SetName(string newname)
    {
        Name = newname;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }
}

}

My XAML

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:app="clr-namespace:App"
    Title="MainWindow" Height="400" Width="400">
<Grid>
    <Grid.Resources>
        <app:Person x:Key="person"/>
    </Grid.Resources>
    <TextBox  Width="100" Height="26" Text="{Binding Source={StaticResource person}, Path=Name, Mode=TwoWay}" />
    <Button Content="Button" Height="23"  Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>

And my codebehind

public partial class MainWindow : Window
{
    Person person;

    public MainWindow()
    {
        InitializeComponent();

        person = new Person();       
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        person.SetName("Jack");
    }
}

Thanks.

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

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

发布评论

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

评论(2

浸婚纱 2024-11-10 00:27:06

您有两个 Person 实例。静态资源中的 PropertyChanged 不为 null

这并不是 StaticResources 的真正用途。摆脱静态资源,将绑定更改为:

{Binding Path=Name, Mode=TwoWay}

并将其添加到构造函数中:

DataContext = person;

You have two instances of Person. PropertyChanged is not null in the static resource

This isn't really what StaticResources are for. Get rid of the static resource, change the binding to:

{Binding Path=Name, Mode=TwoWay}

and add this to your constructor:

DataContext = person;
尤怨 2024-11-10 00:27:06

MainWindow 代码隐藏中的对象 person 与您在 XAML 中绑定的对象不同。

如果您想从资源中使用该对象,则必须在代码隐藏中找到它,因此在构造函数中类似这样

person = (Person)Resources["person"];

That object person in codebehind of MainWindow is not the same object you have binding to in XAML

If you want to use that object from resources you have to find it in code behind so something like this in constructor

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