tags:

views:

19

answers:

1

I have an issue. When I check SelectAllCheckBox all checkbox column in ListView is checked. But when I unchecked one of row checkbox the SelectAllCheckBox is still checked. How to uncheck SelectAllCheckBox if one of row is unchecked? And second question is how highlight ListView rows when SlectAllCheckBox is checked?

My XAML

<Window.Resources>
  <DataTemplate x:Key="CheckBoxCell">
    <StackPanel Orientation="Horizontal">
      <CheckBox Name="chk" IsChecked="{Binding Path=IsSelected, 
        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>
    </StackPanel>
  </DataTemplate>
</Window.Resources>

  <StackPanel Name="Panel1" >
    <StackPanel Grid.IsSharedSizeScope="True">
      <CheckBox Name="SelectAllCheckBox" Margin="5">Select All</CheckBox>
    </StackPanel>
    <ListView Name="ListView1" Background="#f8f8FF" BorderThickness="0" 
               ItemsSource="{Binding Path={}}" Visibility="Visible" Height="Auto" 
               GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler" 
               IsSynchronizedWithCurrentItem="True" 
               VerticalContentAlignment="Center" FontFamily="Tahoma" FontSize="12" 
               Grid.IsSharedSizeScope="False" >
      <ListView.View>
        <GridView AllowsColumnReorder="True" ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
          <GridViewColumn CellTemplate="{StaticResource CheckBoxCell}" Width="30"/>
          <GridViewColumn ... />
          <GridViewColumn ... />
          <GridViewColumn ... />
        </GridView>
      </ListView.View>
    </ListView>
  </StackPanel>

and code behind

  Private Sub ListView1_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ListView1.SelectionChanged
    If ListView1.SelectedItems.Count > 0 Then
      For i As Integer = 1 To ListView1.SelectedItems.Count
        Dim TestDataRow As System.Data.DataRowView
        TestDataRow = ListView1.SelectedItems(i - 1)
      Next
    End If
  End Sub
A: 

I think to fix your highlight problem, you just need to make sure that your checkbox in bound TwoWay to the IsSelected property.

<CheckBox Name="chk" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>

For the select all checkbox, there are a couple of ways your could do it. One way would be to bind the IsChecked property of the checkbox to maybe the SelectedItems property of the listview and then write a converter that checks to see if the selecteditem count is equal to the total row count.

mdm20