C# 绑定到对象的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15610887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
binding to a property of an object
提问by LastBye
I want to binda series of TextBoxesin a grid into properties of an objectwhich is itself another property in my ViewModel (the DataContext).
我想将网格中的一系列 TextBox绑定到一个对象的属性中,该对象本身就是我的 ViewModel(DataContext)中的另一个属性。
CurrentPerson
consists of Name
and Age
properties
CurrentPerson
由Name
和Age
属性组成
Inside the ViewModel:
在视图模型内部:
public Person CurrentPerson { get; set ... (with OnPropertyChanged)}
Xaml :
文件:
<TextBox Text="{Binding Name}" >
<TextBox Text="{Binding Age}" >
I wasn't sure on the approach to use, I set another DataContext in the grid scope, without any result, Also tried setting the source and path like Source=CurrentPerson, Path=Age again without any result, these were for trial and see if there would be any change or not.
我不确定使用的方法,我在网格范围内设置了另一个 DataContext,没有任何结果,还尝试设置源和路径,如 Source=CurrentPerson, Path=Age 再次没有任何结果,这些都是为了试用,看看如果有任何变化。
How should I achieve this ?
我应该如何实现这一目标?
采纳答案by Viv
Does your Person
class members Name
and Age
raise INPC themselves?
你的Person
班级成员Name
和Age
INPC 自己抚养吗?
If you want to update the value of either Name
or Age
in the ViewModel
and have it reflect in the view, you need them to raise property changed individually inside Person
class as well.
如果您想更新Name
或Age
中的值ViewModel
并使其反映在视图中,您还需要它们在Person
类中单独更改属性。
The bindings are fine, but the view is not notified of changes from the view model. Also remember UpdateSourceTrigger
for a TextBox
by default is LostFocus
, so setting that to PropertyChanged
will update your string in the ViewModel
as you're typing.
绑定很好,但是视图不会收到来自视图模型的更改的通知。还要记住UpdateSourceTrigger
,TextBox
默认情况下是LostFocus
,因此将其设置为PropertyChanged
将在ViewModel
您键入时更新您的字符串。
Simple example:
简单的例子:
public class Person : INotifyPropertyChanged {
private string _name;
public string Name {
get {
return _name;
}
set {
if (value == _name)
return;
_name = value;
OnPropertyChanged(() => Name);
}
}
// Similarly for Age ...
}
Now your xaml would be:
现在你的 xaml 将是:
<StackPanel DataContext="{Binding CurrentPerson}">
<TextBox Text="{Binding Name}" />
<TextBox Margin="15"
Text="{Binding Age}" />
</StackPanel>
or you can also bind as suggested by @Kshitij:
或者您也可以按照@Kshitij 的建议进行绑定:
<StackPanel>
<TextBox Text="{Binding CurrentPerson.Name}" />
<TextBox Margin="15"
Text="{Binding CurrentPerson.Age}" />
</StackPanel>
and to update view model as you're typing:
并在您键入时更新视图模型:
<StackPanel DataContext="{Binding CurrentPerson}">
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Margin="15"
Text="{Binding Age, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
回答by K Mehta
Try this:
尝试这个:
<TextBox Text="{Binding CurrentPerson.Name}" />
<TextBox Text="{Binding CurrentPerson.Age}" />
Essentially, you can drill down into properties by using the .
separator.
本质上,您可以使用.
分隔符深入了解属性。
For future reference, if you want to drill down into collections, you can use MyCollection[x]
just like you would in code (where x would be replaced by a hard-coded number, not a variable).
为了将来参考,如果您想深入到集合中,您可以MyCollection[x]
像在代码中一样使用(其中 x 将替换为硬编码数字,而不是变量)。