C# 在数据绑定中访问属性的属性
我的ViewModel中有两个公共属性C# 在数据绑定中访问属性的属性,c#,wpf,data-binding,properties,C#,Wpf,Data Binding,Properties,我的ViewModel中有两个公共属性Foo和BarFoo只是一个字符串,Bar是一个具有公共属性Name的类,该属性是一个字符串 我想将Bar.Name绑定到某个GUI元素我该怎么做? 按预期将字符串Foo写入标签 但是不会将条的名称写入标签中。相反,标签保持为空 编辑: 我的XAML的DataContext(因此,标签的DataContext)被设置为ViewModel EDIT2:当然,真正的代码并不像上面描述的那么简单。我构建了一个仅代表上述描述的最小工作示例: XAML: 事实上,这确
Foo
和Bar
Foo
只是一个字符串,Bar
是一个具有公共属性Name
的类,该属性是一个字符串
我想将Bar.Name
绑定到某个GUI元素我该怎么做?
按预期将字符串Foo
写入标签
但是
不会将条的名称写入标签中。相反,标签保持为空
编辑:
我的XAML的DataContext
(因此,标签的DataContext)被设置为ViewModel
EDIT2:当然,真正的代码并不像上面描述的那么简单。我构建了一个仅代表上述描述的最小工作示例:
XAML:
事实上,这确实起到了预期的作用。由于我无法与您共享实际代码(太多且归公司所有),因此我需要自己搜索原因。欢迎任何关于可能原因的提示 1.Your Model.cs:
public class Model : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
2.您的ViewModel:
namespace MyTestNamespace
{
class MyVM
{
public string Foo { get; set; }
public MyBar Bar { get; set; }
public MyVM()
{
Foo = "I am Foo.";
Bar = new MyBar("I am Bar's name.");
}
}
class MyBar
{
public string Name { get; set; }
public MyBar(string text)
{
Name = text;
}
}
}
public MainViewModel()
{
_model = new Model {Name = "Prop Name" };
}
private Model _model;
public Model Model
{
get
{
return _model;
}
set
{
_model = value;
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
DataContext="{StaticResource MainViewModel}">
<Grid>
<Label Content="{Binding Model.Name}"/>
</Grid>
3.将DataContext设置为ViewModel的视图:
namespace MyTestNamespace
{
class MyVM
{
public string Foo { get; set; }
public MyBar Bar { get; set; }
public MyVM()
{
Foo = "I am Foo.";
Bar = new MyBar("I am Bar's name.");
}
}
class MyBar
{
public string Name { get; set; }
public MyBar(string text)
{
Name = text;
}
}
}
public MainViewModel()
{
_model = new Model {Name = "Prop Name" };
}
private Model _model;
public Model Model
{
get
{
return _model;
}
set
{
_model = value;
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
DataContext="{StaticResource MainViewModel}">
<Grid>
<Label Content="{Binding Model.Name}"/>
</Grid>
1.Your Model.cs:
public class Model : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
2.您的ViewModel:
namespace MyTestNamespace
{
class MyVM
{
public string Foo { get; set; }
public MyBar Bar { get; set; }
public MyVM()
{
Foo = "I am Foo.";
Bar = new MyBar("I am Bar's name.");
}
}
class MyBar
{
public string Name { get; set; }
public MyBar(string text)
{
Name = text;
}
}
}
public MainViewModel()
{
_model = new Model {Name = "Prop Name" };
}
private Model _model;
public Model Model
{
get
{
return _model;
}
set
{
_model = value;
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
DataContext="{StaticResource MainViewModel}">
<Grid>
<Label Content="{Binding Model.Name}"/>
</Grid>
3.将DataContext设置为ViewModel的视图:
namespace MyTestNamespace
{
class MyVM
{
public string Foo { get; set; }
public MyBar Bar { get; set; }
public MyVM()
{
Foo = "I am Foo.";
Bar = new MyBar("I am Bar's name.");
}
}
class MyBar
{
public string Name { get; set; }
public MyBar(string text)
{
Name = text;
}
}
}
public MainViewModel()
{
_model = new Model {Name = "Prop Name" };
}
private Model _model;
public Model Model
{
get
{
return _model;
}
set
{
_model = value;
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
DataContext="{StaticResource MainViewModel}">
<Grid>
<Label Content="{Binding Model.Name}"/>
</Grid>
多亏了Vignesh N.的评论,我才解决了这个问题
在实际代码中,Bar
可以更改,但在开始时它的名称是一个空字符串。这是打开窗口时标签显示的内容。由于当条
属性更改时,标签
不会收到通知,因此它不会更新
解决方案:使ViewModel实现INotifyPropertyChanged
界面,并定义Bar
如下:
private MyBar _bar;
public MyBar Bar
{
get
{
return _bar;
}
set
{
if (_bar != value)
{
_bar = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Bar)));
}
}
}
多亏了Vignesh N.的评论,我才解决了这个问题
在实际代码中,Bar
可以更改,但在开始时它的名称是一个空字符串。这是打开窗口时标签显示的内容。由于当条
属性更改时,标签
不会收到通知,因此它不会更新
解决方案:使ViewModel实现INotifyPropertyChanged
界面,并定义Bar
如下:
private MyBar _bar;
public MyBar Bar
{
get
{
return _bar;
}
set
{
if (_bar != value)
{
_bar = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Bar)));
}
}
}
是否确实已填充viewmodel中Bar类实例的Name属性(并且Bar属性已正确实例化)?标签(或父项之一或DataTemplate)上的DataContext是如何设置的?是的,所有设置都正确。我可以正常使用代码中的Bar.Name
。Bar
和Bar.Name
均不为空。请共享代码以获取更多见解Name
的值是在Bar
的构造函数中或之后设置的,如果是,在设置值后是否引发property changed事件?是否确实已填充viewmodel中Bar类实例的Name属性(并且Bar属性已正确实例化)?标签(或父项之一或DataTemplate)上的DataContext是如何设置的?是的,所有设置都正确。我可以正常使用代码中的Bar.Name
。Bar
和Bar.Name
均不为空。请共享代码以获取更多信息Name
的值是在Bar
的构造函数中设置的还是之后设置的,如果是,在设置值后是否引发属性更改事件?