2013-08-28 114 views
1

我想將ChildProperty綁定到xaml中的T​​extBox。將嵌套對象綁定到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> 

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

回答

0

你得到的錯誤是正確的。您已將DataContext設置爲ParentClass,而您正在設置來自ChildClass的綁定屬性。你只能有一個DataContext。要使用這兩個屬性,您可以在同一個類中定義屬性或從一個屬性派生,並使用子類作爲datacontext。

+0

我想有單獨的類這些屬性:

更改爲文本框的結合。我無法在同一個類中定義兩個屬性。 – user2323308

+0

如果屬性是不同的類,綁定的唯一方法是使用RelativeSource或Element Name在可視樹中跳轉。 –

+0

爲什麼不在ChildClass中派生類並將其設置爲DataContext? –

0

父類中定義ChildClass類型的屬性如下:

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

在父類的構造函數的初始化_childClass的實例。

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

感謝