【问题标题】:PropertyChanged event handler is always nullPropertyChanged 事件处理程序始终为空
【发布时间】:2012-06-08 09:23:40
【问题描述】:

当我第一次在其构造函数中将投标分配给标签时,标签会正确绑定并根据 CurrentMarket 类的当前 ComponentData 值显示正确的信息。但是,当 ComponentData 更改时,OnPropertyChanged 事件触发正常,但 ProperyChanged 处理程序始终为 NULL。有人可以建议我做错了什么吗?

我有一个标签,我这样设置绑定:

    public StyledLabel(string Property, int i)
    {
        Binding BindingText = new System.Windows.Data.Binding(Property);
        BindingText.Source = Statics.CurrentMarket.ComponentData;
        BindingText.Converter = new TextConverter();
        this.SetBinding(Label.ContentProperty, BindingText);

     }

当前市场类别如下所示:

public class CurrentMarket : INotifyPropertyChanged
{
    string sMarket = "";
    ComponentData cComponentData;

    public string Market
    {
        set
        {
            sMarket = value;
            OnPropertyChanged("Market");
            ComponentData = SharedBoxAdmin.Components[sMarket];
        }
        get
        {
            return sMarket;
        }
    }

    public ComponentData ComponentData
    {
        get { return cComponentData; }
        set
        {
            cComponentData = value;
            OnPropertyChanged("ComponentData");
        }
    }

    public CurrentMarket()
    {
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

 }

谢谢!

【问题讨论】:

    标签: c# wpf binding event-handling


    【解决方案1】:

    尝试将要绑定的属性名称指定为BindingPath(而不是Source 的一部分):

    Binding BindingText = new System.Windows.Data.Binding(Property);
    BindingText.Source = Statics.CurrentMarket;
    BindingText.Path = new PropertyPath("ComponentData");
    BindingText.Converter = new TextConverter();
    this.SetBinding(Label.ContentProperty, BindingText);
    

    【讨论】:

      猜你喜欢
      • 2018-02-03
      • 2010-12-03
      • 2016-09-16
      • 1970-01-01
      • 2017-12-12
      • 2014-06-15
      • 2013-07-17
      • 1970-01-01
      相关资源
      最近更新 更多