Ferhat
Ferhat

Reputation: 491

Checkbox in gridviewcolumn(header)

I am using a listview (gridview/gridviewcolumn) where the first column contains only checkboxes for each row. Instead of adding a select all button I want to add a Checkbox into the header of the first column.

Selecting the checkbox in the header will select all other checkboxes and vice versa.

How can I do that in xaml?

Update: This is the important part of my xaml code. (simplified)

<ListView ItemSource="...">
<ListView.View>
<GridView>

<GridViewColumn>
  <GridViewColumn.CellTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
         <CheckBox IsChecked="{Binding IsSelected}" />
      </StackPanel>
    </DataTemplate>
  </GridViewColumn.CellTemplate>
</GridViewColumn>

</GridView>
</ListView.View>
</ListView>

Upvotes: 3

Views: 8064

Answers (1)

Haris Hasan
Haris Hasan

Reputation: 30097

To have a checkbox on top of GridViewColumn you can do something like this

<GridViewColumn>

 <GridViewColumn.Header>
     <CheckBox/>
  </GridViewColumn.Header>

  <GridViewColumn.CellTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
         <CheckBox IsChecked="{Binding IsSelected}" />
      </StackPanel>
    </DataTemplate>
  </GridViewColumn.CellTemplate>
</GridViewColumn>

Now you can handle the check event and in the code you can iterate through your itemsSource and change values or if you are following MVVM you can bind property with checkbox so that whenever check is changed you will be notified through INotifyPropertyChanged. Once you find out through binding that check has changed, again you can change your itemssource accordingly

Upvotes: 10

Related Questions