2013-02-02 38 views
1

我無法獲取mainwindow.xaml文本框中另一個類的屬性。在MainWindow.xaml中,我試圖獲取定義的屬性值在mainwindow.xaml.cs.Here我成功地獲取ABC類的名稱屬性在第一textbox.Details低於:無法在mainwindow.xaml文本框中獲取其他類的屬性

MainWindow.xaml

<Window x:Class="WpfApplication7.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" Name="UI"> 
    <Grid DataContext="{Binding}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="100"/> 
      <RowDefinition Height="100"/> 
     </Grid.RowDefinitions> 
     <StackPanel Orientation="Vertical" Grid.Row="0"> 
     <TextBox Text="{Binding Name1}"/> 
     </StackPanel> 
     <StackPanel Orientation="Vertical" Grid.Row="1"> 
      <TextBox Text="{Binding class1.Name1}"/> 
     </StackPanel> 
    </Grid> 
</Window> 

MainWindow.xaml.cs

public partial class MainWindow : Window,INotifyPropertyChanged 
{ 
    Class1 c1 = new Class1(); 
    public MainWindow() 
    { 
     InitializeComponent(); 

ABC win = new ABC(); win.Name1 =「主窗口」;
c1.Name =「Class 1」; this.DataContext = win; }

public class ABC 
    { 
     public string Name { get; set; } 
    } 

    public Class1 class1 
    { 
     get 
     { 
      return c1; 
     } 
     set 
     { 
      c1 = value; 
      INotifyChanged("class1"); 
     } 
    } 

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

的Class1.cs

public class Class1:INotifyPropertyChanged 
{ 
    private string name; 
    public string Name 
    { 
     get 
     { 
      return name; 
     } 
     set 
     { 
      name = value; 
      NotifyPropertyChanged("Name"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void NotifyPropertyChanged(string property) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 
} 
+0

沒有名爲'Name1'在'Class1' –

+0

<文本框的文本=「{屬性Binding class1.Name1}「/> – Parmod

+1

看看你的'Class1'它有1個名爲Name的屬性,所以你的xaml必須是'' –

回答

1

Class1它已經1個屬性稱爲Name所以你的XAML將不得不<TextBox Text="{Binding class1.Name}"/>,你似乎是設置你的DataContext到嵌套類,你不能這樣做,xaml不支持嵌套類。

您必須添加ABC作爲一個變量並取出作爲嵌套類

例子:

的XAML:

<Window x:Class="WpfApplication7.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" Name="UI"> 
    <Grid DataContext="{Binding ElementName=UI}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="100"/> 
      <RowDefinition Height="100"/> 
     </Grid.RowDefinitions> 

     <!--You can't bind to a nested class--> 
     <!--<StackPanel Orientation="Vertical" Grid.Row="0"> 
      <TextBox Text="{Binding Name1}"/> 
     </StackPanel>--> 

     <StackPanel Orientation="Vertical" Grid.Row="0"> 
      <TextBox Text="{Binding ABCClass.Name1}"/> 
     </StackPanel> 
     <StackPanel Orientation="Vertical" Grid.Row="1"> 
      <TextBox Text="{Binding class1.Name}"/> 
     </StackPanel> 
    </Grid> 
</Window 

代碼:

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    private Class1 c1 = new Class1(); 
    private ABC _abcClass = new ABC(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     class1.Name = "Class 1"; 
     _abcClass.Name1 = "ABC Class"; 
    } 

    public ABC ABCClass 
    { 
     get { return _abcClass; } 
     set { _abcClass = value; INotifyChanged("ABCClass"); } 
    } 

    public Class1 class1 
    { 
     get { return c1; } 
     set { c1 = value; INotifyChanged("class1"); } 
    } 

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

public class Class1 : NotifyBase 
{ 
    private string name; 
    public string Name 
    { 
     get { return name; } 
     set { name = value; NotifyPropertyChanged("Name"); } 
    } 
} 

public class ABC : NotifyBase 
{ 
    private string name; 
    public string Name1 
    { 
     get { return name; } 
     set { name = value; NotifyPropertyChanged("Name1"); } 
    } 
} 

public class NotifyBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public void NotifyPropertyChanged(string property) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 
} 

結果:

enter image description here