xena12 xena12 -4 years ago 109
C# Question

How to check from xaml if any of the checkbox is checked c#

I want to enable button until all fields are filled and I did that but I don't know how to do that with checkboxes. I need at least one of them to be checked and it has to be done from xaml not code behind.
If anyone can help I'll appreciate it.

Here is my xaml code:

<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding Text.Length, ElementName=firstname, UpdateSourceTrigger=PropertyChanged}" Value="0">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding Text.Length, ElementName=lastname, UpdateSourceTrigger=PropertyChanged}" Value="0">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding Text.Length, ElementName=username, UpdateSourceTrigger=PropertyChanged}" Value="0">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding Text.Length, ElementName=password, UpdateSourceTrigger=PropertyChanged}" Value="0">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, ElementName=scientist}" Value="false">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, ElementName=writer}" Value="false">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, ElementName=programmer}" Value="false">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>


EDIT: This will enable button only when all the check boxes are checked, but I need it to be enabled when at least one of them is checked.

Answer Source

You need a MultiDataTrigger...

            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding IsChecked, ElementName=scientist}" Value="false"/>
                    <Condition Binding="{Binding IsChecked, ElementName=writer}" Value="false"/>
                    <Condition Binding="{Binding IsChecked, ElementName=programmer}" Value="false"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="IsEnabled" Value="False"/>
            </MultiDataTrigger>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download