INotifyPropertyChanged的 - 事件保持空 [英] INotifyPropertyChanged - Event stays null

查看:116
本文介绍了INotifyPropertyChanged的 - 事件保持空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下INotifyPropertyChanged的扩展:

I am trying to implement the following INotifyPropertyChanged Extension:

自动INotifyPropertyChanged的(接受的答案)
的http:// ingebrigtsen。信息/ 2008/12/11 / INotifyPropertyChanged的-再访/

但我无法弄清楚,为什么我的PropertyChanged事件处理保持空。 (

But I cant figure out why my PropertyChanged EventHandler stays null. :(

我做了一个很简单的WPF应用程序来测试它,这是我的XAML代码:

I did a very simple WPF Application to test it out, here is my XAML Code:

<StackPanel Orientation="Vertical">
    <TextBox Text="{Binding Path=SelTabAccount.Test, UpdateSourceTrigger=PropertyChanged}"></TextBox>
    <TextBox Text="{Binding Path=SelTabAccount.TestRelated, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>

和我后面的代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private TabAccount _selTabAccount;

    public TabAccount SelTabAccount
    {
        get { return _selTabAccount; }
        set
        {
            _selTabAccount = value;
            PropertyChanged.Notify(() => this.SelTabAccount);
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        SelTabAccount = new TabAccount()
        {
            Test = "qwer",
            TestRelated = ""
        };
    }
}

public partial class TabAccount : INotifyPropertyChanged
{
    private string _test;

    public string Test
    {
        get { return _test; }
        set
        {
            _test = value;
            PropertyChanged.Notify(() => this.Test);
            PropertyChanged.Notify(() => this.TestRelated);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public partial class TabAccount
{
    private string _testRelated;

    public string TestRelated
    {
        get
        {
            _testRelated = Test + "_Related";
            return _testRelated;
        }
        set
        {
            _testRelated = value;
            PropertyChanged.Notify(() => this.TestRelated);
        }
    }
}

在你后面的代码将见一个类有2个属性应该通知属性的变化,但没有任何反应(其部分只是随机测试)。

In the code behind you will see one class (its partial for just random testing) with 2 properties that should notify a property change but nothing happens.

该NotificationExtension是一个副本,并从提供的链接粘贴在顶部,是一个外部的CS文件。

The NotificationExtension is a copy and paste from the links provided at top and is in a external cs file.

我也试图与正常INotifyPropertyChanged的实施做样品,这按预期工作,但我不能使它发生这种扩展类。

I have also tried to do the sample with the "normal" INotifyPropertyChanged implementation and this works as expected but I cant make it happen with this extension class.

希望你能帮助我找到答案。
先谢谢了。

Hope you can help me figure it out. Thanks in advance.

推荐答案

绑定只有当你提供一些数据源的视觉对象将正常工作。如果你不提供任何数据源,并希望挖掘到绑定将无法工作的属性。

Binding will work only when you provide some datasource to the visual objects. If you don't provide any datasource and want to dig into the properties the binding will not work.

在你的主窗口构造函数,窗口的DataContext属性设置为数据源。例如:

Under your MainWindow Constructor, set the DataContext property of the Window to a datasource. For example:

 public MainWindow()
 {
    InitializeComponent();

   // your property setups

    this.DataContext = this; 
 }

从本质上讲这使得主窗口的属性可用于绑定的主窗口项目可视化树

Essentially this makes the MainWindow properties available for binding to the visual tree of MainWindow items.

这篇关于INotifyPropertyChanged的 - 事件保持空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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