Note that there are some explanatory texts on larger screens.

plurals
  1. POCheckbox to select all or deselect all in a listview not bound to dataset
    primarykey
    data
    text
    <p>I have a report page which displays a view for a set of data that we report off of. This view has about 40 fields. The user can query and filter the data to get what they need and then export it to an excel report which is then automatically emailed to management that needs to see the information.</p> <p>Because not all of the fields are needed for every report I also have a list box next to the view with checkboxes to hide or show any fields. I have been trying to figure out how to get all checkboxes checked or unchecked by selecting checkbox in header. The xaml for the list box is bound to the IsVisible property of the fields in the grid and looks like so.</p> <pre><code>&lt;ListView ItemsSource="{Binding Columns, ElementName=reportGrid}" SelectionMode="Multiple"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Width="50"&gt; &lt;GridViewColumn.HeaderTemplate&gt; &lt;DataTemplate&gt; &lt;CheckBox x:Name="checkall" IsChecked="{Binding IsMainSelected}" Tag="{Binding Source={StaticResource Spy}, Path=DataContext}" /&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.HeaderTemplate&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate &gt; &lt;CheckBox IsChecked="{Binding IsVisible}" /&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Header="Field" DisplayMemberBinding="{Binding Header}" Width="100"&gt;&lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; </code></pre> <p></p> <p>The reference to the Spy static resource came from this article. <a href="http://www.codeproject.com/Articles/27432/Artificial-Inheritance-Contexts-in-WPF" rel="nofollow">http://www.codeproject.com/Articles/27432/Artificial-Inheritance-Contexts-in-WPF</a></p> <p>However, the problem I am encountering with every solution is the fact that the checkboxes in the listview are bound to a dataset. In this case the checkboxes are bound to the visibility of a column in a grid (on object on a page). Everything I have tried has failed. It's probably a simple solution but darned if I can figure it out. Any help would be appreciated. </p> <p>UPDATE: I came up with a solution to this problem using code behind. If there is a way to do this without using code behind I am all ears, but this was fairly simple and straight forward number one and number two it doesn't really break the rules of keeping logic out of code behind.</p> <p>The xaml</p> <pre><code>&lt;ListView x:Name="lv" ItemsSource="{Binding Columns, ElementName=reportGrid}" Grid.Column="0" Grid.Row="1" Margin="5,160,25,30" SelectionMode="Multiple"&gt; &lt;ListView.View&gt; &lt;GridView x:Name="gl"&gt; &lt;GridViewColumn Width="50"&gt; &lt;GridViewColumn.CellTemplate &gt; &lt;DataTemplate x:Name="fieldChecked" &gt; &lt;CheckBox IsChecked="{Binding IsVisible}" /&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;CheckBox Margin="0" x:Name="chkSelectAll" Click="chkSelectAll_Click" IsChecked="True" /&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Header="Check/Uncheck All" DisplayMemberBinding="{Binding Header}" Width="110"&gt;&lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>The code behind</p> <pre><code> private void chkSelectAll_Click(object sender, RoutedEventArgs e) { if (chkSelectAll.IsChecked.Value == true) { for (int i = 0; i &lt; lv.Items.Count; i++) { ListViewItem lvi = (ListViewItem)lv.ItemContainerGenerator.ContainerFromItem(lv.Items[i]); if (lvi != null) { CheckBox c = lvi.FindChildByType&lt;CheckBox&gt;(); c.IsChecked = true; } } } else { for (int i = 0; i &lt; lv.Items.Count; i++) { ListViewItem lvi = (ListViewItem)lv.ItemContainerGenerator.ContainerFromItem(lv.Items[i]); if (lvi != null) { CheckBox c = lvi.FindChildByType&lt;CheckBox&gt;(); c.IsChecked = false; } } } } </code></pre> <p>One caveat was that the list view I had to scroll down to bottom to see all of the fields. If I didn't do this it wouldn't uncheck all of the fields. I also had to put in the line of code if(lvi != null) as well because if you didn't scroll down it would think an item in the list was null and crash. </p> <p>Anyway it isn't perfect but that is how I fixed it. Any better solutions using MVVM that isn't so blanking complicated to implement please enlighten me.</p> <p>Thanks</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.
    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