1

xaml で ChildProperty を 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

ChildClass以下のように、ParentClassの Type のプロパティを定義します。

    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 に答える