.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Binding to nested property
 

Binding to nested property

Is binding to a property of property supported? I have a class with a property that returns another class. That class has a property I want to bind to, but I want to set my DataContext to the main class.
public class ChildObj
{
public string Name {...}
}

public class ParentObj : INotifyPropertyChanged
{
public ChildObj FirstChild {...}

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(...)
{
...
}

}

In my main window xaml I have a binding to "FirstChild.Name" as well as other bindings to other properties in ParentObj. In the window code I set the container's DataContext to an instance of ParentObj. When the app runs everything looks fine. All fields have the appropriate values.
The problem is that I cannot seem to get the FirstChild.Name updated. I have some other app event that changes FirstChild.Name and I then call OnPropertyChanged with "FirstChild.Name" and the control does not get updated. If I call OnPropertyChanged with "" then it does get updated - along with all other fields which I really don't want.

Am I doing something wrong or is this really not supposed to done this way?


John Miller
JGMiller
I think N level of indirections supported in binding,
But you are not calling OnPropertyChanged correctly.

You should call OnPropertyChanged in childObj as well when its Name proprty changes. Calling OnPropertyChanged with "FirstChild.Name " on ParentObj will not work

public class ChildObj
{
public string Name {...}

public void OnPropertyChanged(...)
{
...
}
}
  • Proposed As Answer byhbarck Tuesday, September 22, 2009 6:40 PM
  • Marked As Answer byJGMiller Tuesday, September 22, 2009 7:10 PM
  •  
Himanshu Rakibe
Hi,

By way of explanation:

The PropertyChanged event is always local to the object that raises it, i.e. it notifies only about changes in this object's properties. OnPropertyChanged("") is probably interpreted as "All properties of this object have changed", so the system assumes that FirstChild is a new instance and re-reads all its properties, which of course includes the Name property.
  • Marked As Answer byJGMiller Tuesday, September 22, 2009 7:10 PM
  •  
hbarck
I think N level of indirections supported in binding,
But you are not calling OnPropertyChanged correctly.

You should call OnPropertyChanged in childObj as well when its Name proprty changes. Calling OnPropertyChanged with "FirstChild.Name " on ParentObj will not work

public class ChildObj
{
public string Name {...}

public void OnPropertyChanged(...)
{
...
}
}
  • Proposed As Answer byhbarck Tuesday, September 22, 2009 6:40 PM
  • Marked As Answer byJGMiller Tuesday, September 22, 2009 7:10 PM
  •  
Himanshu Rakibe

Thanks, tThat makes sense and I will give it a try. I had initially thought of that but when I saw the fields were properly displayed then I just assumed WPF was always going to traverse the individual properties in the property name. It did it in the initial binding but I guess it doesn't on the OnPropertyChanged(). That also doesn't explain why calling OnPropertyChanged() with "" updates all fields including the nested ones.

Ifthis works then I will use it -but it still seems inconsistent.


John Miller
JGMiller
Hi,

By way of explanation:

The PropertyChanged event is always local to the object that raises it, i.e. it notifies only about changes in this object's properties. OnPropertyChanged("") is probably interpreted as "All properties of this object have changed", so the system assumes that FirstChild is a new instance and re-reads all its properties, which of course includes the Name property.
  • Marked As Answer byJGMiller Tuesday, September 22, 2009 7:10 PM
  •  
hbarck

You can use google to search for other answers

Custom Search

More Threads

• Binding DataContext in GridView child control
• MediaElement Repeat/Replay behaviour
• Login Sourc Code Sample
• Creating Brush,Pens and Geometries in a non-UI thread
• Can u change the name of generic.xaml?
• DocumentViewer.Print does not allow me to select page range
• Extend the last column in a GridView
• DependencyProperty issue.
• How to set Togglebutton's background where it has checked
• Unloaded event firing on a UserControl when it shouldn't???