1

Let's say I have a MVVM application with a ListView of Items and a TextBox editing a property (Text) of a selected item.

<TextBox Text="{Binding SelectedItem.Text}"/>
<ListBox ItemsSource="{Binding Items}" DisplayMemberPath="Text" SelectedItem="{Binding SelectedItem}"

In my ViewModel I have a ObservableCollection of Items. Each item implements the INotifyPropertyChanged and has the requiered property. If I change the text of my TextBox, the List is not updating because I have only a property without notification:

public class Item : INotifyPropertyChanged
{
    public string Text { get; set; }
}

My question is: Is there a way to update the Text-property of my list without using the PropertyChanged like this?:

private string _Text;
public string Text
{
  ...
  set
  {
    _Text = value;
    PropertyChanged("Text");
  }
}

Is there a way to fire the PropertyChanged direct from my View after changed the Text of my TextBox - or a different approach?

Thank you for your help!

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • Where is your `TextBox`? I see only `TextBlock`. Why it's not possible to raise `PropertyChanged` event from view model? – dkozl Apr 17 '16 at 09:47
  • Go back to the now deleted comment you left below, and follow the link you left. Then read what that answer says. Then compare that to your `Item` class with its `Text` property. The answer is staring you in the face. – slugster Apr 17 '16 at 09:52
  • @dkozl: I changed the TextBlock to TextBox ... my mistake. Without implementing the INotifyPropertyChanged it worked without any notification (Look here: http://stackoverflow.com/questions/7767218/why-does-the-binding-update-without-implementing-inotifypropertychanged). Now I implemented the INPC, and I don't want to add a notification on each property of my project (This is only a minimal example) – Fruchtzwerg Apr 17 '16 at 09:53
  • @slugster: I have read it several times ... but I have not really a idea how to use it. Could you give me a hint? – Fruchtzwerg Apr 17 '16 at 09:54

1 Answers1

0

Okay, I'll leave an answer instead of a slightly cryptic comment (which I did because you actually learn better if you discover the answer yourself rather than being spoonfed...).

The previous link you left states:

And if you are implementing INotifyPropertyChanged, you are fully responsible to implement the change notification in every setter of the properties which needs to be data bound to the UI. Otherwise, the change will be not synchronized as you'd expect.

(Emphasis mine. For the sake of completion, here is the link that was left with that answer: MSDN forums: Data binding without INotifyPropertyChanged).

Previously you had relied on the default behavior of WPF, but when you implement INotifyPropertyChanged you are effectively saying "Yo WPF!! I want to be really specific and particular about what gets notified, so that default behavior can take a seat at the back!". You then failed to add a notification on the Text property. So as per your coding decision, WPF binding no longer detects property changes in that class unless you specifically trigger them.

Personally I always implement INotifyPropertyChanged because then property changes are still notified even when the value is changed via code (because the PropertyDescriptor used by default doesn't detect this). Additionally it gives me the flexibility to not notify, or to notify several properties if one changes.

Community
  • 1
  • 1
slugster
  • 49,403
  • 14
  • 95
  • 145
  • Thank you for your answer @slugster. This is a very clear description. The link describes that WPF is automatically binding to a PropertyDescriptor without the INPC. In my case I not have to detect a change of the Text-property out of code. Is there a way or a workaround to use the INPC for my whole object and the WPF-automatic mechanism for only this single Text-property? – Fruchtzwerg Apr 17 '16 at 10:09
  • No, there's no way to do both like that. Using INPC means you take full control. You can streamline the process though, check [this old answer](http://stackoverflow.com/a/13720780/109702) of mine which removes a bit of the drudgery (the code in that answer was pre .Net 4.5 and CallerMemberName). That's also just an example, you can extend that approach in a bunch of [different ways](http://blog.amusedia.com/2013/06/inotifypropertychanged-implementation.html). – slugster Apr 17 '16 at 10:30
  • 1
    Thank you for your great answer! That was not really the answer I wanted to hear for my project ... but now I know what's the exact problem. – Fruchtzwerg Apr 17 '16 at 11:26