views:

501

answers:

5

I have a object in a presenter connected to a view. Inside my XAMTL I have the following:

<Label Content="{Binding ElementName=PSV, Path=Presenter.Portfolio.Name}"/>

Now when the control is created, Portfolio is null, then i run another method which sets Portfolio. I've implemented INotifyPropertyChanged but so far, I've not been able to trigger to hookup to the binding.

Can someone give me tips? Can I bind to a property of a property ?

A: 

If you have implemented INotifyPropertyChanged correctly, this will work just fine. Some things to try:

  1. Try setting a dummy portfolio with a dummy name in your presenter's constructor so you can make sure the binding is actually correct.
  2. If it still doesn't work after #1, look in your output window for any binding errors.
  3. If it did work after #1, make sure the portfolio you are setting has a name at the time it was constructed. If not, your portfolio class will also need to implement INotifyPropertyChanged and raise the event when the Name is set.

Failing all that, please post your code.

HTH, Kent

Kent Boogaart
A: 

I don't think it would be sufficient what you do if during binding Portfolio is null. Even if the "Portfolio" property was strongly typed and telling that INotifyPropertyChanged may be implemented, how could WPF register for the "PropertyChanged" event if it only finds a null reference?

Try making "Portfolio" a Dependency Property. This kind of property has mechanisms in place that would allow WPF to notice when the actual value of "Portfolio" changes. The actual "Portfolio" instance then implements the INotifyPropertyChanged interface as you have done to inform WPF that the Name property has changed.

Also, yes you can bind to properties of properties of ...

flq
It is sufficient. Dependency properties are WPF specific and shouldn't be placed in your domain layer.
Kent Boogaart
A: 

Since you have INotifyPropertyChanged implemented, are you making sure to fire the PropertyChanged event in your Portfolio.Name setter?

string _name;
public string Name
{
get 
{
   return _name;
}
set
{
   _name = value;
   // Alert the databinding engine about changes to the source value
   OnPropertyChanged("Name");
}

void OnPropertyChanged(string propertyName)
{
   if (PropertyChanged != null)
      PropertyChanged(propertyName);
}

#region INotifyPropertyChanged members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
KP Adrian
+1  A: 

Binding always works with the DataContext you would need to set your Presenter into the local DataContext. For example you could do this in the constructor of your Window or UserControl:

this.DataContext = new Presenter();

Your binding would then change to:

<Label Content="{Binding ElementName=PSV, Path=Portfolio.Name}"/>

The Presenter part of your former path is implicit in the DataContext.

This way the DataContext is watching for the NotifyChanged events and will update the view properly when Portfolio changes from null to having a value.

In answer to the final part of your question, binding to a property of a property does work.

Doug Ferguson