I have an strange problem with binding in WPF
. There is a simple sample of what i'm doing:
public class Project
{
private string _title;
public string Title
{
get { return _title; }
set
{
_title= value;
RaisePropertyChanged("Title");
}
}
}
public class People
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name= value
RaisePropertyChanged("Name");
}
}
private Project _project;
public Project Project
{
get { return _project; }
set
{
_project= value;
RaisePropertyChanged("Project");
}
}
}
Now I bound a grid to an instance of People in the view and it can bind controls to Project and Name of People class, but I really can't understand why I can not bind to Project.Title
.
I write my XAML
code like this:
<TextBox Text="{Binding Name}"/>
<Combobox .... SelectedItem="{Binding Project}"/>
<TextBox Text="{Binding Project.Title}"/>
The first two controls above get bounded correctly but the last TextBox
doesn't. I have no idea why it can access to Project
but not Project.Title
? It's an another weird thing I've already seen in WPF!
Perhaps your combo box selection is not setting the selected value without using Mode=TwoWay
:
<Combobox .... SelectedItem="{Binding Project, Mode=TwoWay}"/>
Once the Project
is set, the property is set, the Title
will show.
Try "Path=Project.Title". Worked for me in a same case.
if your DataContext is an instance of your Person object it should work. you can check your bindings at runtime with Snoop ( http://snoopwpf.codeplex.com/ ) btw. give it a try :)
you can also do
<TextBox DataContext="{Binding Project, Mode=OneWay}" Text="{Binding Title}"/>
谢谢大家,但问题不是我的想法。实际上我使用PropertyChanged.Fody将INotifyPropertyChanged
注入属性,但似乎未达到我的预期。我自己实现了INotifyPropertyChanged
,现在可以正常工作。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.