1

当我第一次在其构造函数中将投标分配给标签时,标签会正确绑定并根据 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));
        }
    }

 }

谢谢!

4

1 回答 1

3

尝试将要绑定的属性名称指定为PathBinding而不是 的一部分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);
于 2012-06-08T09:35:16.253 回答