Binding to Class item and no set executed

62 Views Asked by At

In my ViewModel I have:

private MyType item;
public MyType Item
{
    get { return item; }
    set {
          if(item == null)
            return;
          item = value;
          OnPropertyChanged(()=>Item);
        }
}

In my view:

<TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Path=Item.Name, UpdateSourceTrigger=PropertyChanged}" />

Is there any possibility to to trigger set of Item while typing in textbox? Or I should create Property for every field of class?

3

There are 3 best solutions below

0
On

What you have done is to notify only when the address of MyType changes not the property with in this type. So if you need the changes to be notified with in a property in a type then those properties should explicitly throw onpropertychanged event.

0
On

Consider adding OnPropertyChanged() with specific parameter in the set block of MyType's "Name" property which notifies about entire object change, not "Name" only.

0
On

E.g

private string _name;
public string Name
{
   get { return _name; }
}
{
    set 
    {
        _name = value;
        OnPropertyChanged();
        OnPropertyChanged("Item");
    }
}

private MyType item;
public MyType Item
{
    get { return item; }
    set {
          if(item == null)
            return;
          item = value;
          OnPropertyChanged(()=>Item);
        }
}