Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is happening because for TwoWay bindings, WPF only detects when the property gets a new value, not when an object referenced by the property changes.</p> <p>In your case your property contains the ObservableCollection created by your converter. Although the contents of the ObservableCollection has been modified, and it fires INotifyPropertyChanged, the bound property itself has not changed: It still refrences the same ObservableCollection as before. Because of this, WPF DataBinding is not triggered and your source is not updated.</p> <p>When you call UpdateSource() manually, it forces the ObservableCollection to be passed through your converter and back to your data object, so it works.</p> <p>The easiest way to get the behavior you desire is:</p> <ol> <li><p>Instead of binding to the data field, bind to the data object, and extract the desired field in the converter (if you want to make a generic converter that can access any field, pass the field as a parameter).</p></li> <li><p>In the converter when you construct the ObservableCollection, add a CollectionChanged event that updates the original object whenever it fires.</p></li> </ol> <p>Here is the general idea in code:</p> <pre><code> public MyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { PropertyInfo property = object.GetType().GetProperty((string)parameter); var coll = BinaryToCollection((Binary)property.GetValue(object, null)); coll.CollectionChanged += (sender, e) =&gt; { property.SetValue(object, CollectionToBinary(coll)); }; return coll; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } private ObservableCollection&lt;SomeType&gt; BinaryToCollection(Binary data) { // conversion details here } private Binary CollectionToBinary(ObservableCollection&lt;SomeType&gt; coll) { // conversion details here } } </code></pre> <p>In this case your binding would change from</p> <pre><code> &lt;ItemsControl ItemsSource="{Binding something.property, Mode=TwoWay, Converter={...}}" </code></pre> <p>to</p> <pre><code> &lt;ItemsControl ItemsSource="{Binding something, Converter={...}}" </code></pre> <p>with the converter parameter being the property name</p> <p>Hope this helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload