Note that there are some explanatory texts on larger screens.

plurals
  1. POUnwanted Selection Events In ListView
    primarykey
    data
    text
    <p>How do I prevent a ListView selection change event from firing when I only want the bound data to change?</p> <p>To clarify, I have classes like this:</p> <pre><code>public class MyCollectionViewModel : BindableBase { public ObservableCollection&lt;MyItemViewModel&gt; MyItems { get; set; } private MyItemViewModel _selectedItem; public MyItemViewModel SelectedItem { get { return _selectedItem; } set { this.SetProperty(ref _selectedItem, value); if (_selectedItem != null) { // Do something... } } } ... } public class MyItemViewModel : BindableBase { private bool _isSelected; public bool IsSelected { get { return _isSelected; } set { this.SetProperty(ref _isSelected, value); } } ... } </code></pre> <p>With the following data template:</p> <pre><code>&lt;DataTemplate x:Key="MyItemTemplate"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;ColumnDefinition Width="*"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;CheckBox Margin="10" IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" /&gt; &lt;Border Grid.Column="1"&gt; ... &lt;/Border&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; </code></pre> <p>... and used like this:</p> <pre><code>&lt;ListView ItemsSource="{Binding Source={StaticResource SessionsViewSource}}" ItemTemplate="{StaticResource MyItemTemplate}" SelectedItem="{Binding Sessions.SelectedItem, Mode=TwoWay}" IsSwipeEnabled="False" /&gt; </code></pre> <p>To somewhat visualize the ListView:</p> <blockquote> <pre><code> +--------------------- + [x] myItem1 +--------------------- + [ ] myItem2 +--------------------- </code></pre> </blockquote> <p>The user can check and uncheck the checkboxes. Problem is, if there's an existing selection, when the <code>IsSelected</code> binding updates, the selection binding happens as well.</p> <p>Is there a simple way to fix this? I'd like to stick with MVVM as much as possible.</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. 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