Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is one solution we currently came up with. It has the advantage that it separates different tasks to the appropriate layer. For example, the View enforces an update of the binding, while the ViewModel tells the View to do so. Another advantage is that its handled synchronously, which would for example allow to check the content right before switching away, and the call-stack remains unchanged without raising "External Code" (Going over <code>Dispatcher</code> or even <code>DispatcherTimer</code> would do so) which is better for maintenance and flow control. A disadvantage is the new Event which has to be bound and handled (and finally unbound. I present an anonymous handler only for example reasons).</p> <p><strong>How to get there?</strong></p> <p>In <code>ViewModelBase</code>, implement a new <code>ForceBindingUpdate</code> event:</p> <pre><code>public abstract class ViewModelBase : INotifyPropertyChanged { // ----- leave everything from original code ------ public event EventHandler ForceBindingUpdate; protected void OnForceBindingUpdate() { var handler = ForceBindingUpdate; if (handler != null) handler(this, EventArgs.Empty); } } </code></pre> <p>In <code>MainViewModel</code>, update the setter of the <code>SelectedItem</code> property:</p> <pre><code>set // of SelectedItem Property { if (_selectedViewModel != value) { // Ensure Data Update - the new part OnForceBindingUpdate(); // Old stuff _selectedViewModel = value; OnPropertyChanged("SelectedItem"); } } </code></pre> <p>Update the <code>MvvmTestView</code> Code Behind to implement the new event:</p> <pre><code>void MvvmTestView_Loaded(object sender, RoutedEventArgs e) { // remains unchanged Mvvm.MainViewModel viewModel = new Mvvm.MainViewModel(); viewModel.Items.Add(new Mvvm.ItemViewModel("Hello StackOverflow")); viewModel.Items.Add(new Mvvm.ItemViewModel("Thanks to Community")); // Ensure Data Update by rebinding the content property - the new part viewModel.ForceBindingUpdate += (s, a) =&gt; { var expr = ContentTextBox.GetBindingExpression(TextBox.TextProperty); expr.UpdateSource(); }; // remains unchanged DataContext = viewModel; } </code></pre> <p>Last but not least, the minimal XAML Update: Give the <code>TextBox</code> a name by adding <code>x:Name="ContentTextBox"</code> Attribute to the <code>TextBox</code>s XAML.</p> <p>Done.</p> <p>Actually, I don't know if this is the cleanest solution, but it gets close to what we had in mind.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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