【发布时间】:2023-03-16 19:12:01
【问题描述】:
如何在类属性中访问类中的属性。 Value2 有效,Value1 无效。如果我在 codeBehind 中绑定 Value1,则 Value1 有效。实际代码:
class Class1 : INotifyPropertyChanged
{
private string _Value1;
public string Value1
{
get { return _Value1; }
set { _Value1 = value; NotifyPropertyChanged("Value1"); }
}
[...]
}
Class2.cs:
class Class2 : INotifyPropertyChanged
{
private string _Value2;
public string Value2
{
get { return _Value2; }
set { _Value2 = value; NotifyPropertyChanged("Value2"); }
}
public Class2()
{
Class1 class1 = new Class1();
class1.Value1 = "Value 1 set";
Value2 = "Value2 set";
}
[...]
}
MainWindow.xaml.cs
public partial class MainWindow : Window
{
Class2 class2 = new Class2();
public MainWindow()
{
InitializeComponent();
this.DataContext = class2;
}
}
MainWindow.xaml
<Grid>
<TextBox Text="{Binding Path=class1.Value1}" />
<TextBox Text="{Binding Path=Value2}" />
</Grid>
【问题讨论】:
标签: c# wpf datacontext