NoWar
NoWar

Reputation: 37633

Check a checkbox based on checkbox selection in WPF

There is a checkbox that is already binded to boolean field "IsOutsourcing"

<CheckBox x:Name="chkIsOutsourcing" IsChecked="{Binding IsOutsourcing, Mode=TwoWay}" />

And I need to check it when checked another checkbox.

<CheckBox x:Name="chkIsOption1" IsChecked="{Binding IsOption1, Mode=TwoWay}" />

How ot can be done with XAML?

Can we use here multiple elements to bind?

IsChecked="{Binding IsOutsourcing chkIsOption1, Mode=TwoWay}"

Thanks!

Upvotes: 0

Views: 48

Answers (1)

Jawahar
Jawahar

Reputation: 4885

This can be done using MultiBinding with MultiValueConverter.

<CheckBox x:Name="chkIsOutsourcing">
    <CheckBox.IsChecked>
        <MultiBinding Converter="{StaticResource BooleanConverter}">
            <Binding Path="IsOutSourcing" />
            <Binding Path="IsChecked"
                        ElementName="chkIsOption1" />
        </MultiBinding>
    </CheckBox.IsChecked>
</CheckBox>

The Converter,

public class BooleanConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool value1 = (bool)values[0];
        bool value2 = (bool)values[1];

        return value1 || value2;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 1

Related Questions