C# 无法在mainwindow.xaml文本框上获取另一个类的属性
我无法在mainwindow.xaml文本框中获取另一个类的属性。在mainwindow.xaml中,我尝试获取在mainwindow.xaml.cs上定义的属性值。在此,我成功获取第一个文本框中ABC类的Name属性。详细信息如下: main window.xamlC# 无法在mainwindow.xaml文本框上获取另一个类的属性,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我无法在mainwindow.xaml文本框中获取另一个类的属性。在mainwindow.xaml中,我尝试获取在mainwindow.xaml.cs上定义的属性值。在此,我成功获取第一个文本框中ABC类的Name属性。详细信息如下: main window.xaml <Window x:Class="WpfApplication7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/present
<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>
ABC win=新ABC();
win.Name1=“主窗口”c1.Name=“类别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 partial class MainWindow : Window,INotifyPropertyChanged
{
Class1 c1 = new Class1();
public MainWindow()
{
InitializeComponent();
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));
}
}
}
Class1
它有一个名为Name
的属性,因此您的xaml必须是
,并且您似乎正在将DataContext
设置为嵌套类。您不能这样做,xaml中不支持嵌套类
您必须将ABC
作为变量添加,并将其作为嵌套类删除
例如:
Xaml:
班级
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));
}
}
}
结果:
Class1
它有一个名为Name
的属性,因此您的xaml必须是
,并且您似乎正在将DataContext
设置为嵌套类。您不能这样做,xaml中不支持嵌套类
您必须将ABC
作为变量添加,并将其作为嵌套类删除
例如:
Xaml:
班级
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));
}
}
}
结果:
在
Class1
中没有名为Name1
的属性,看看你的Class1
它有一个名为Name
的属性,所以你的xaml必须是
是的,你是对的……它是错的……现在我按照你说的做了……但它仍然没有显示我想要什么,你修复了吗DataContext
就像我在回答中建议的那样?在Class1
中没有名为Name1
的属性看看你的Class1
它有一个名为Name
的属性,所以你的xaml必须是
是的,你是对的……它是错的……现在我按照你说的做了……但它仍然没有显示出来我想要你按照我在回答中的建议修复你的DataContext
?
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));
}
}
}