Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everybody, I have a GroupBox with it's DataContext binded to an object:

DataContext = Customers.CurrentCustomer

CurrentCustomer is an Object wicht implements IChangeNotifyProperty. The group box contains two TextBox with its text property binded to properties of CurrentCustomer

<textbox name="Name" text="{Binding Path=Name}" />
<textbox name="Telephone" text="{Binding Path=Telephone}" />


The first time I assign the DataContext it works properly it shows the data but if I do the following:

C#
Customers.CurrentCustomer = otherCustomer


the TextBoxs don't reflect this change. In fact the DataContext is pointing to the first CurrentCustomer it's like it makes a copy of it instead of having a pointer to it. If I do the following

C#
Customers.CurrentCustomer = otherCustomer;
DataContext = Customers.CurrentCustomer;


the text boxes are refreshed properly. My question is if there is a way to avoid the reassigment of the DataContext and it gets notified every time CurrentCustomer changed.

Excuse me if the question is too obvious, I'm new in WPF...

Thanks for any help.
Posted
Comments
Keith Barrow 8-May-11 9:15am    
"Excuse me if the question is too obvious, I'm new in WPF"
The only stupid question is the one you don't ask. And "Snd Codez plz" :-).

It took me a little while to spot what is happening. The reason for your problem is you are changing the reference to the CurrentCustomer on the Customers object in a way that won't re-bind the UI. To see what I mean:

Have Customers implement the INotifyProperyChanged Interface. I normally do the following:
public abstract class ViewModel: INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Then:
public class Customers : ViewModel
{
   Customer _customer;

   public Customer Customer 
   {
       get { return _customer; }
       set 
       {
          if(_customer == value)
              return;
          _customer = customer;
          NotifyPropertyChanged("Customer");
       }
   }
}

Then
DataContext = Customers;


And change your binding paths, the UI will now be notified if the CurrentCustomer object changes on the Customers instance
 
Share this answer
 
As Keith has pointed out, the problem lay in the fact that you changed your DataContext after the binding has occurred. As a general pointer, you should never do this because the binding engine will no longer respond to the binding events.
 
Share this answer
 
Hello Keith, thanks for your help, it's exactly what I'm looking for. Pete I apreciate your comment.
 
Share this answer
 
Comments
Pete O'Hanlon 8-May-11 17:16pm    
No problem. You may want to accept Keith's answer so that everybody knows it's been solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900