Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight MVVM binding updates fire in undesired order
    primarykey
    data
    text
    <p><strong>Scenario</strong>: In a Silverlight 4 MVVM project, we have a <code>ListBox</code> control containing items, the selected item is two-way-bound to the appropriate property in the ViewModel. Another control (for example reasons, I've stripped it down to a single <code>TextBox</code>) is data bound to the selected item's content. The value should update on leave/focus lost.</p> <p><strong>Problem</strong>: When the value in the <code>TextBox</code> is changed and we leave that <code>TextBox</code> by pressing the Tab key, everything works as desired - the value is updated. However, if the user clicks on a different item in the <code>ListBox</code>, then the SelectedItem setter is fired before the content of <code>TextBox</code> setter is fired, leaving no chance to handle the user input.</p> <p><img src="https://imgur.com/NuEc0.png" alt="Screen"></p> <p>You can see in debugger, when adding breakpoints to the property setters, that the new <code>ListView</code> selection is applied first, before the <code>TextBox</code> update is processed.</p> <p><strong>Desired behavior</strong>: We need to know that the currently selected item was modified before the user has selected another item. It's not desired to have a custom update trigger which would notify on each key press (we know that's possible).</p> <p>Can you help?</p> <p><strong>Code (a very simple example):</strong></p> <p><strong>ViewModel</strong></p> <pre><code>public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } } public class ItemViewModel : ViewModelBase { private string _content; public ItemViewModel(string initContent) { _content = initContent; } public string Content { get { return _content; } set { if (_content != value) { _content = value; OnPropertyChanged("Content"); } } } } public class MainViewModel : ViewModelBase { private ObservableCollection&lt;ItemViewModel&gt; _items = new ObservableCollection&lt;ItemViewModel&gt;(); private ItemViewModel _selectedViewModel; public ObservableCollection&lt;ItemViewModel&gt; Items { get { return _items; } } public ItemViewModel SelectedItem { get { return _selectedViewModel; } set { if (_selectedViewModel != value) { _selectedViewModel = value; OnPropertyChanged("SelectedItem"); } } } } </code></pre> <p><strong>XAML</strong></p> <pre><code>&lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;ListBox Height="100" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" DisplayMemberPath="Content" Width="220" /&gt; &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="12,118,0,0" Text="{Binding SelectedItem.Content, Mode=TwoWay}" VerticalAlignment="Top" Width="220" /&gt; &lt;/Grid&gt; </code></pre> <p><strong>XAML Code Behind</strong></p> <pre><code> public MvvmTestView() { InitializeComponent(); Loaded += new RoutedEventHandler(MvvmTestView_Loaded); } void MvvmTestView_Loaded(object sender, RoutedEventArgs e) { MainViewModel viewModel = new MainViewModel(); viewModel.Items.Add(new ItemViewModel("Hello StackOverflow")); viewModel.Items.Add(new ItemViewModel("Thanks to Community")); DataContext = viewModel; } </code></pre> <p><strong>UPDATE 1</strong> I present <a href="https://stackoverflow.com/questions/5338546/silverlight-mvvm-binding-updates-fire-in-undesired-order/5394399#5394399">a self designed solution</a> for you to check out, which will be probably be the accepted one, I still want to encourage you to make comments and give your hints. Thanks.</p>
    singulars
    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.
 

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