views:

1101

answers:

1

Hi.

I'd like to implement a select all checkbox in xaml.
I have several (templated) checkboxes in a listview. Then I have a checkbox outside of the listview, which I want to have a "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 checkbox doesn't (directly) have anything 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 see the IsChecked Property of the SelectSingle is already bound to my ViewModel. So I reckon I need a trigger to manipulate the state of the checkbox.

Now I already tried sth 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>

I also tried the same thing within a style, but to no avail. I always obtain an error, sth along the lines of "static member "IsCheckedProperty couldn't be found in type "ContentPresenter"".

Now that sounds as if the Target/SourceName binding wouldn't work, but why? Is there something that I am missing?

A: 

Torsten, I am sorry if I didn't understand what you've tried already, but you need to bind the IsChecked property of the CheckBoxes inside the ListView to the IsChecke property of the CheckBox outside it using IsChecked="{Binding Path=IsChecked, Mode=OneWay,ElementName=OutsideCheckBox}"

Gustavo Cavalcanti
no sorry. it's not quite what I want. The "single" checkboxes are already bound to my ViewModel objects as you can see in the first listing. now, I also want to bind the single checkbox IsChecked Property to the "all" checkbox IsChecked Property.I'm fiddling around with MultiBinding atm, but I haven't solved this, yet.
Torsten