XAML WPF可视性绑定到多变量布尔表达式

bmvo0sr5  于 2022-12-16  发布在  其他
关注(0)|答案(4)|浏览(211)

我有两个布尔值,我想根据它们的值显示Image,如下所示:

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>

<Image Visibility="{Binding (Boolean1 && Boolean2),Converter={StaticResource BooleanToVisibilityConverter}}" />

注意布尔1和布尔2表达式。

vcirk6k6

vcirk6k61#

XAML中没有定义&&运算符,但您可以绑定到多个属性并使用IMultiValueConverter

<Image>
    <Image.Visibility>
        <MultiBinding Converter="{StaticResource YourMultiConverter}">
            <Binding Path="Boolean1" />
            <Binding Path="Boolean2" />
        </MultiBinding>
    </Image.Visibility>
</Image>
public class YourMultiValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool a = (bool)values[0];
        bool b = (bool)values[1];

        return a && b ? Visibility.Visible : Visibility.Collapsed;
    }

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

或者,您可以使用带条件的Image样式:

<Image>
    <Image.Style>
        <Style TargetType="Image">
            <Setter Property="Visibility" Value="Collapsed" />
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Boolean1}" Value="True" />
                        <Condition Binding="{Binding Boolean2}" Value="True" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Visibility" Value="Visible" />
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>
kzmpq1sx

kzmpq1sx2#

mm8已经提供了正确答案,但是这将是一个微小的改进

public class LogicalAndConverter : IMultiValueConverter
{
    public IValueConverter FinalConverter{get;set;}

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool rtn = values.All(v=>(bool)v);

        if(FinalConverter==null)    
            return rtn;
        else
            return FinalConverter.Convert(rtn,targetType,parameter,culture);
    }

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

这就允许您使用不同的转换器,而无需重写多路转换器

<local:LogicalAndConverter x:Key="LogicalAndConverter ">
    <local:LogicalAndConverter.FinalConverter>
        <BooleanToVisibilityConverter />
    </local:LogicalAndConverter.FinalConverter>
</local:LogicalAndConverter>
hs1ihplo

hs1ihplo3#

如果您不介意在项目中添加另一个包,DevExpress的MVVM框架(可以免费获得)有一个名为DXBinding的绑定标记扩展,允许您编写绑定函数/表达式。

<Image Visibility="{DXBinding 'Boolean1 and Boolean2', Converter={StaticResource BooleanToVisibilityConverter}}"/>
7cjasjjr

7cjasjjr4#

可以使用多值转换器解决此问题
XAML:

<TextBlock Name="textBlockOutput" Grid.Row="4" Grid.Column="2">
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource MultiValueConverter}">
                <Binding Path="TextOne" />
                <Binding Path="TextTwo" />
                <Binding Path="TextThree" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>



 public class MultiValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string one = values[0] as string;
        string two = values[1] as string;
        string three = values[2] as string;
        if(!string.IsNullOrEmpty(one) && !string.IsNullOrEmpty(two) && !string.IsNullOrEmpty(three))
        {
            return one + two + three;
        }
        return null;
    }

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

相关问题