我无法在 mainwindow.xaml 文本框上获取另一个类的属性。在 MainWindow.xaml 中,我试图获取在 mainwindow.xaml.cs 上定义的属性值。这里我成功获取了第一个文本框中的 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}">
<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>
主窗口.xaml.cs
public partial class MainWindow : Window,INotifyPropertyChanged
{
Class1 c1 = new Class1();
public MainWindow()
{
InitializeComponent();
ABC赢=新ABC();win.Name1 = "主窗口";
c1.Name = "Class 1"; this.DataContext = 赢;}
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));
}
}
}