XAML绑定到拥有对象的属性

时间:2015-01-26 15:46:00

标签: c# wpf xaml silverlight binding

我正在编写Silverlight应用程序。我的MainPage对象拥有另一个实现INotifyPropertyChanged接口的对象,并具有bool CLR属性IsIncluded:

public partial class MainPage: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    CustomObject customObject = ... // was created successfully

    CheckBox checkBox = ... // is actually created in in XAML

    // this is only for testing purposes
    public bool IsIncluded
    {
        get { ... }
        set {
            // ...
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("IsIncluded"));
        }
    }
}

public partial class CustomObject: INotifyPropertyChanged
{
    public bool IsIncluded
    {
        get { ... }
        set {
            // ...
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("IsIncluded"));
        }
    }
}

我的MainPage.xaml包含

<CheckBox IsChecked="{Binding Path=IsIncluded,Mode=OneWay}" />
<CheckBox IsChecked="{Binding Path=customObject.IsIncluded,Mode=OneWay}" />

第一个绑定工作正常。第二个没有。我需要第二个工作。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

CustomObject _customObject = ... // was created successfully
    public CustomObject customObject
    {
        get { return _customObject ; }
        set {
            _customObject  = value;

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("customObject"));
            }
    }
相关问题