属性更改事件处理程序始终为空

本文关键字:程序 事件处理 属性 | 更新日期: 2023-09-27 18:36:29

当我第一次在其构造函数中将出价分配给标签时,标签会正确绑定并根据当前市场类的当前组件数据值显示正确的信息。但是,当 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));
        }
    }
 }

谢谢!

属性更改事件处理程序始终为空

尝试将要绑定到的属性名称指定为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);