Search code examples
c#wpfxamlcode-behind

Get ListViewItem control in CodeBehind


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>

Solution

  • 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.