1

我想将 ChildProperty 绑定到 xaml 中的 TextBox。

XAML:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="ChildProperty" Grid.Column="0" Grid.Row="0"/>
        <TextBox  Text="{Binding Path=ChildProperty}" Grid.Column="1" Grid.Row="0" Width="50"/>
        <TextBlock Text="ParentProperty" Grid.Column="0" Grid.Row="1"/>
        <TextBox Text="{Binding Path=ParentProperty}" Grid.Column="1" Grid.Row="1" Width="50"/>
    </Grid>

数据上下文:

public NotifyParentChangePropertyInChildClass()
        {
            InitializeComponent();
            this.DataContext = new ParentClass();
        }

亲子班:

public class ParentClass :INotifyPropertyChanged
    {
        private int parentProperty;
        public int ParentProperty
        {
            get { return parentProperty; }
            set 
            { 
                parentProperty = value;
                RaisePropertyChanged("ParentProperty");
            }
        }
        public ParentClass()
        {
            ChildClass obj = new ChildClass();
            obj.ChildProperty = 100;
            parentProperty = 200;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class ChildClass : INotifyPropertyChanged
    {
        private int childProperty;
        public int ChildProperty
        {
            get { return childProperty; }
            set 
            { 
                childProperty = value;
                RaisePropertyChanged("ChildProperty");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

当我运行上面的代码时,输​​入输出窗口消息"System.Windows.Data Error: 40 : BindingExpression path error: 'ChildProperty' property not found on 'object' ''ParentClass' (HashCode=59593954)'. BindingExpression:Path=ChildProperty; DataItem='ParentClass' (HashCode=59593954); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')"

4

2 回答 2

0

您得到的错误是正确的。您已将 DataContext 设置为 ParentClass,而您正在从 ChildClass 设置绑定属性。您只能拥有一个 DataContext。要使用这两个属性,您可以在同一个类中定义属性或从一个类派生并将子类用作数据上下文。

于 2013-08-28T08:30:18.627 回答
0

在 ParentClass 中定义 Type 的属性ChildClass如下:

    ChildClass _childClass;
    public ChildClass ChildClass
    {
        get { return _childClass; }
        set { _childClass = value; RaisePropertyChanged("ChildClass"); }
    }

并在 ParentClass 的构造函数中初始化 _childClass 的实例。

将文本框的绑定更改为:

<TextBox  Text="{Binding Path=**ChildClass.ChildProperty**}" Grid.Column="1" Grid.Row="0" Width="50"/>

谢谢

于 2013-08-28T08:34:53.843 回答