将复选框可见性绑定到转换后的bool和另一个复选框可见性

时间:2022-06-01 08:45:47

Below binds the visibility of my checkbox to the converted bool. That works fine. How would i add a second condition? I only want to make the checkbox visible if the converted bool is true and another checkbox called Allowed is checked.

下面将复选框的可见性绑定到转换后的bool。工作的很好。如何添加第二个条件?如果转换后的bool为true,而另一个名为allow的复选框被选中,那么我只想让复选框可见。

            <CheckBox Grid.Row="3" Foreground="Black" Grid.ColumnSpan="2" x:Name="IsItComplete" IsThreeState="False"
                  BorderBrush="Black" VerticalContentAlignment="Center" 
                  Checked="IsItComplete_Checked" 
                  Style="{StaticResource CheckBoxStyle1}" 
                  Visibility="{Binding Job.CanItBeComplete, Converter={StaticResource booleanToVisibilityConvertor}, 
                  Mode=OneWay, 
                  Source={StaticResource Locator}}">
            <CheckBox.Content>
                <TextBlock Text="Is It Complete" Margin="0"/>
            </CheckBox.Content>
        </CheckBox>

2 个解决方案

#1


3  

This can be solved via two approaches i can think of over my head (ofcourse having single property in View model already suggested so i am not going to talk about it again)

这可以通过我能想到的两种方法来解决(当然,视图模型中有一个属性已经建议过了,所以我不会再讨论它了)

Approach 1

Bind Visibility of checkBox with MultiValueConverter in place. Pass two bindings to it:

使用MultiValueConverter绑定复选框的可见性。传递两个绑定到它:

  1. Actual bool property of class.
  2. 类的实际bool性质。
  3. IsChecked DP of Allowed checkbox.
  4. IsChecked DP of Allowed复选框。

Converter will do AND operation on two values and will return accordingly.

转换器将对两个值执行操作并相应地返回。

        <CheckBox>
            <CheckBox.Visibility>
                <MultiBinding Converter="{StaticResource MyConverter}">
                    <Binding Path="IsEnable"/>
                    <Binding ElementName="Allowed" Path="IsChecked"/>
                </MultiBinding>
            </CheckBox.Visibility>
        </CheckBox>

MutliValueConverter code:

MutliValueConverter代码:

public class MyConverter: IMultiValueConverter
{
    public object Convert(object[] values, Type targetType,
                          object parameter, CultureInfo culture)
    {
        return ((bool)values[0] && (bool)values[1])
                 ? Visibility.Visible : Visibility.Collapsed;
    }

    public object[] ConvertBack(object value, Type[] targetTypes,
                                object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

Approach 2

Use MultiDataTrigger in checkBox style like this:

在复选框样式中使用MultiDataTrigger,如下所示:

<CheckBox>
   <CheckBox.Style>
      <Style TargetType="CheckBox">
         <Setter Property="Visibility" Value="Collapsed"/>
         <Style.Triggers>
            <MultiDataTrigger>
               <MultiDataTrigger.Conditions>
                  <Condition Binding="{Binding IsEnable}" Value="True"/>
                  <Condition Binding="{Binding ElementName=Allowed,
                                           Path=IsChecked}" Value="True"/>
               </MultiDataTrigger.Conditions>
               <Setter Property="Visibility" Value="Visible"/>
            </MultiDataTrigger>
         </Style.Triggers>
      </Style>
    </CheckBox.Style>
 </CheckBox>

#2


4  

If you are working under MVVM, one approach is to create a property in the ViewModel which will handle the logic and return a boolean indicating if the checkbox should be visible or not.

如果在MVVM下工作,一种方法是在ViewModel中创建一个属性,该属性将处理逻辑并返回一个布尔值,指示复选框是否应该可见。

The second check box will have to be bound to a property as well, and be sure to do a TwoWay binding so that the property is updated when the checkbox is checked or not.

第二个复选框也必须绑定到属性,并确保执行TwoWay绑定,以便在选中复选框时更新属性。

This should help: Bind two elements' Visibility to one property

这应该会有所帮助:将两个元素的可见性绑定到一个属性

#1


3  

This can be solved via two approaches i can think of over my head (ofcourse having single property in View model already suggested so i am not going to talk about it again)

这可以通过我能想到的两种方法来解决(当然,视图模型中有一个属性已经建议过了,所以我不会再讨论它了)

Approach 1

Bind Visibility of checkBox with MultiValueConverter in place. Pass two bindings to it:

使用MultiValueConverter绑定复选框的可见性。传递两个绑定到它:

  1. Actual bool property of class.
  2. 类的实际bool性质。
  3. IsChecked DP of Allowed checkbox.
  4. IsChecked DP of Allowed复选框。

Converter will do AND operation on two values and will return accordingly.

转换器将对两个值执行操作并相应地返回。

        <CheckBox>
            <CheckBox.Visibility>
                <MultiBinding Converter="{StaticResource MyConverter}">
                    <Binding Path="IsEnable"/>
                    <Binding ElementName="Allowed" Path="IsChecked"/>
                </MultiBinding>
            </CheckBox.Visibility>
        </CheckBox>

MutliValueConverter code:

MutliValueConverter代码:

public class MyConverter: IMultiValueConverter
{
    public object Convert(object[] values, Type targetType,
                          object parameter, CultureInfo culture)
    {
        return ((bool)values[0] && (bool)values[1])
                 ? Visibility.Visible : Visibility.Collapsed;
    }

    public object[] ConvertBack(object value, Type[] targetTypes,
                                object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

Approach 2

Use MultiDataTrigger in checkBox style like this:

在复选框样式中使用MultiDataTrigger,如下所示:

<CheckBox>
   <CheckBox.Style>
      <Style TargetType="CheckBox">
         <Setter Property="Visibility" Value="Collapsed"/>
         <Style.Triggers>
            <MultiDataTrigger>
               <MultiDataTrigger.Conditions>
                  <Condition Binding="{Binding IsEnable}" Value="True"/>
                  <Condition Binding="{Binding ElementName=Allowed,
                                           Path=IsChecked}" Value="True"/>
               </MultiDataTrigger.Conditions>
               <Setter Property="Visibility" Value="Visible"/>
            </MultiDataTrigger>
         </Style.Triggers>
      </Style>
    </CheckBox.Style>
 </CheckBox>

#2


4  

If you are working under MVVM, one approach is to create a property in the ViewModel which will handle the logic and return a boolean indicating if the checkbox should be visible or not.

如果在MVVM下工作,一种方法是在ViewModel中创建一个属性,该属性将处理逻辑并返回一个布尔值,指示复选框是否应该可见。

The second check box will have to be bound to a property as well, and be sure to do a TwoWay binding so that the property is updated when the checkbox is checked or not.

第二个复选框也必须绑定到属性,并确保执行TwoWay绑定,以便在选中复选框时更新属性。

This should help: Bind two elements' Visibility to one property

这应该会有所帮助:将两个元素的可见性绑定到一个属性