jeudi 4 février 2016

Can the Setter of a DataTrigger modify a property within the DataContext?

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.

<DataTemplate>
    <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>

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?

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




Aucun commentaire:

Enregistrer un commentaire