将嵌套对象绑定到TextBox

时间:2013-08-28 08:22:54

标签: c# wpf xaml data-binding

我想将ChildProperty绑定到xaml中的T​​extBox。

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>

的DataContext:

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')"

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在ParentClass中定义类型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"/>

由于