Select all checkbox in XAML using trigger?

I would like to select select all in xaml.
I have several (templated) checkboxes in a listview. Then I have a checkbox outside the list, and I want to have "select all" -behaviour. I could easily solve the problem in my ViewModel, however, I think it would be more elegant to do this in xaml, since the select all flag (directly) has nothing to do with my ViewModel. The code looks something like this:

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
        <CheckBox Content="Globale Eingabe"
            Name="SelectSingle"
            IsChecked="{Binding IsChecked}">
        </CheckBox>
    </DataTemplate>
<ListView.ItemTemplate>  
</ListView>
<CheckBox Name="SelectAll" />

As you can see, the IsChecked property for SelectSingle is already bound to my ViewModel. Therefore, I believe that I need a trigger to manipulate the state of the flag.

Now I have tried it like this:

<CheckBox Content="Globale Eingabe"
    Name="SelectSingle"
    IsChecked="{Binding IsChecked}">
    <CheckBox.Triggers>
        <Trigger SourceName="SelectAll" Property="IsChecked" Value="True">
            <Setter TargetName="SelectSingle"  Property="IsChecked" Value="True"/>
        </Trigger>
    </CheckBox.Triggers>
</CheckBox>

or sth like this:

<CheckBox Content="Globale Eingabe"
    Name="SelectSingle"
    IsChecked="{Binding IsChecked}">
    <CheckBox.Triggers>
        <DataTrigger Binding="{Binding ElementName=SelectAll, Path=IsChecked}" 
            Value="True">
            <Setter TargetName="Check"
                Property="IsChecked"
                Value="True"/>
        </DataTrigger>
    </CheckBox.Triggers>
</CheckBox>

, . , sth " ". IsCheckedProperty "ContentPresenter".

, Target/SourceName , ? -, ?

+3
2

, Check All ViewModel . WPF ( TreeView, ListView) ViewModel : " Smarts ViewModel" .
, ViewModel, MultiBinding, , .
- : -)

+3

Torsten, , , , IsChecked CheckBox ListView IsChecked CheckBox , :

IsChecked="{Binding Path=IsChecked, Mode=OneWay,ElementName=OutsideCheckBox}"
0

Source: https://habr.com/ru/post/1708320/


All Articles