0
votes

I have a check box which I plan to implement a 'Select All' feature on.

I figured the easiest implementation was to modify my DataTemplate with a DataTrigger to change the IsChecked property to true/false.

<UserControl.Resources>
    <DataTemplate x:Key="MyTemplate">
        <Grid>
            <CheckBox x:Name="Selection" IsChecked="{Binding Selected}" />
        </Grid>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding ElementName=SelectAll, Path=IsChecked}" Value="true">
                  <Setter TargetName="Selection" Property="DataContext.Selected" Value="true" />
            </DataTrigger>
           <DataTrigger Binding="{Binding ElementName=SelectAll, Path=IsChecked}" Value="false">
                   <Setter TargetName="Selection" Property="IsChecked" Value="false" />
           </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</UserControl.Resources>

<Grid>
  <CheckBox Name="SelectAll />
  <ListView Name="lvSteps"  ItemTemplate="{StaticResource MyTemplate}" ItemsSource="{Binding MyList}" />
</Grid>

However, this overwrites the binding of the CheckBox which is set to the DataContext property 'Selected' to just the bool values set in the DataTrigger.

Which thinking about it makes perfect sense.

So my question is, can I change the value of DataContext property within a DataTrigger via Setter?

So I can keep my CheckBox bound to 'Selected', and change the 'Selected' value within the Setter of the DataTrigger?

1

1 Answers

1
votes

you maybe think about it in the wrong way.

SelectAll is surly part of your container Element (lets say ListVM) which intern holds your objects (ListItemVM) which contains Selected so if you check SelectAll you can and should iterate over your Elements and set Selected to true and this items use there INotifyPropertyChanged to inform your CheckBox


Edit for Comment

there is no need to hook up the event of SelectAll just bind to it and do your thing in the Set-Part

public bool SelectAll
{
   get { return _selectAll;}
   set 
   {
       _selectAll = value;
       RaisPropertyChanged();

       foreach( var item in MyList)
       {
            item.Select = _selectAll;
       }
   }    
}

Also why would you like to do this in xaml?

Because you could use interfaces and factor it out in a way that you can us it at many places without code redundancy also you will be able to do it in code from where ever you are able to access the "ListVM"