Reputation: 621
I have a listview and each Item has a CheckBox control as part of its ItemTemplate.
<ListView x:Name="taskListView" Grid.Row="2" BorderThickness="0" Margin="30,0,0,0" ItemsSource="{Binding ChildItems}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="290"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" HorizontalAlignment="Center"></CheckBox>
<TextBlock Text="{Binding Name}" MaxWidth="270" Grid.Column="1" Margin="0,0,10,0"/>
<ComboBox SelectedItem="{Binding DependentTask, Mode=TwoWay}"
Grid.Column="2"
Margin="0,3,0,3"
ItemsSource="{Binding DependentTasks, Converter={StaticResource addEmptyItemConverter}}"
HorizontalAlignment="Left"
MinWidth="150"
DisplayMemberPath="ProjectionTaskLink.Name"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
There is a parent to this ListView that has a checkbox as well. When that parent checkbox is checked, I want to check all the checkboxes in the ListViewItems. How can I get a hold of those in Codebehind so i can set them to Checked or Unchecked depending on the parent condition?
Thanks.
EDIT: Here is the full XAML:
<Grid Margin="0,10,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="35"/>
<ColumnDefinition Width="35"/>
</Grid.ColumnDefinitions>
<CheckBox HorizontalAlignment="Center" Grid.Column="0" Checked="CheckBox_Checked"/>
<TextBlock Text="{Binding Name}" Grid.Column="1"/>
<TextBlock Text="Loops " Grid.Column="2" TextAlignment="Center"/>
<TextBox Text="{Binding Scenarios}" Grid.Column="3"/>
</Grid>
<TextBlock Visibility="{Binding ContainsProjectionTasks, Converter={StaticResource boolToVisibilityConverter}}"
HorizontalAlignment="Left"
Width="450"
TextAlignment="Right"
VerticalAlignment="Bottom"
Grid.Row="1"
Text="Task Dependencies"
Background="White"/>
<ListView x:Name="taskListView" Grid.Row="2" BorderThickness="0" Margin="30,0,0,0" ItemsSource="{Binding ChildItems}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="290"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" HorizontalAlignment="Center"></CheckBox>
<TextBlock Text="{Binding Name}" MaxWidth="270" Grid.Column="1" Margin="0,0,10,0"/>
<ComboBox SelectedItem="{Binding DependentTask, Mode=TwoWay}"
Grid.Column="2"
Margin="0,3,0,3"
ItemsSource="{Binding DependentTasks, Converter={StaticResource addEmptyItemConverter}}"
HorizontalAlignment="Left"
MinWidth="150"
DisplayMemberPath="ProjectionTaskLink.Name"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
Upvotes: 2
Views: 2291
Reputation: 621
I ended up just changing the ListView ItemsSource collection in the setter of the IsChecked property of the parent check box, in my ViewModel. This eliminated any need for codebehind. Coupled with the NotifyPropertyChanged, it works now.
Upvotes: 0
Reputation: 3735
In your listview itemssource make a property for checkbox in listview, and bind it to the property.
Then you can write this code in your parent checkbox's Checked and UnChecked events:
foreach(var item in myItemsSource)
item.IsChecked=ParentCheckBox.IsChecked.GetValueOrDefault();
good luck.
Upvotes: 1
Reputation: 4999
Have the elements of ChildItems implement INotifyPropertyChanged.
Add a property to those elements called IsChecked, which raises a NotifyPropertyChangedEvent in the setter.
Bind your ListViewItem's Checkbox to the IsChecked Property
I assume you are already handling the checked event on the parent checkbox. So all you need to do now is add this to the codebehind method:
(foreach YourType item in ChildItems)
item.IsChecked = parentCheckbox.IsChecked;
Or you could try (where parentCheckboxName is the x:Name of the master checkbox.
<CheckBox IsChecked="{Binding Path=IsChecked, ElementName=parentCheckboxName}" Grid.Column="0" HorizontalAlignment="Center" />
Upvotes: 1
Reputation: 37770
Bind these two checkboxes in markup:
<CheckBox IsChecked="{Binding Path=IsChecked, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type YourAncestorType}}}" Grid.Column="0" HorizontalAlignment="Center" />
There's no need to use codebehind here.
Upvotes: 0