Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Gridview Checkbox Column Header MVVM
    primarykey
    data
    text
    <p>I am still wrapping my head around this whole MVVM pattern but i thought i had a good grasp on it until i attempted to create a Checkbox column for my Gridview. I need the user to be able to select all items listed(via the header checkbox) or select the items listed individually. I databound the IsChecked property of my checkboxes to two boolean fields on my viewmodel. The checkbox on the cell template works as expected and fires the property changed event. The header does nothing. What am i missing here. Again this is still new to me so be gentle. Also if there is something i should be doing, or a better way to accomplish this...im all ears. </p> <p>Thanks</p> <p><strong>XAML</strong></p> <pre><code> &lt;UserControl x:Class="CheckBoxDemo.GridDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"&gt; &lt;Grid&gt; &lt;ListView ItemsSource="{Binding PersonList}"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Width="50"&gt; &lt;GridViewColumn.HeaderTemplate&gt; &lt;DataTemplate&gt; &lt;CheckBox IsChecked="{Binding IsMainSelected}"/&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.HeaderTemplate&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;CheckBox IsChecked="{Binding IsSelected}"/&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="100"&gt;&lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/Grid&gt; </code></pre> <p></p> <p><strong>ViewModel</strong></p> <pre><code>class GridDemoViewModel:INotifyPropertyChanged { public List&lt;Person&gt; PersonList { get; private set; } // Fields... private bool _isMainSelected; public bool IsMainSelected { get { return _isMainSelected; } set { _isMainSelected = value; NotifyPropertyChanged("IsMainSelected"); } } public GridDemoViewModel() { PersonList = new List&lt;Person&gt;(); PersonList.Add(new Person { Name = "John"}); PersonList.Add(new Person { Name = "Tom" }); PersonList.Add(new Person { Name = "Tina" }); PersonList.Add(new Person { Name = "Mary" }); PersonList.Add(new Person { Name = "Mia"}); PersonList.Add(new Person { Name = "Crystal" }); } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } } </code></pre> <p><strong>Person Class</strong></p> <pre><code>public class Person:INotifyPropertyChanged { public string Name { get; set; } // Fields... private bool _isSelected; public bool IsSelected { get { return _isSelected; } set { if (_isSelected == value) return; _isSelected = value; NotifyPropertyChanged("IsSelected"); } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } } </code></pre>
    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