如何布尔值 &&两个可见性转换器 [英] How to boolean && two visibility converters

查看:18
本文介绍了如何布尔值 &&两个可见性转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单独的可见性转换器,一个基于字段是否已更新,另一个基于是否允许查看字段.我为页面上的每个文本项使用了 updatedField ,以便在更新的字段旁边显示一颗星.但某些文本项根据权限级别仅对某些用户可见.

I have two separate converters for visibility, one based on whether a field has been updated and one based on whether a field is allowed to be seen. I use the updatedField one for each text item on my page so that a star shows up next to an updated field. But some text items only are visible to some users based on permission levels.

例如:

<Image Visibility="{Binding ElementName=MyObject, Path=UpdatedFields, Mode=OneWay, Converter={StaticResource updatedFieldConverter}, ConverterParameter=FieldToTest}" Source="Properties:Resources.star_yellow" />

<TextBlock FontSize="21" Foreground="{DynamicResource LabelBrush}" Text="{x:Static Properties:Resources.Some_Text}" Visibility="{Binding Source={StaticResource allowedFields}, Path=Some_Text_Field, Converter={StaticResource visibilityConverter}}" />

我的问题是,对于需要许可的字段,我需要运行两个转换器来确定星星是否出现.有没有办法对两个转换器的结果执行布尔值与"?

My problem is that for the case of the permission-required fields I need to run both converters to determine if the star shows up. Is there a way to do a boolean "And" on the results of two converters?

我看了这篇文章但它似乎不允许将不同的参数集传递给两个不同的转换器.

I looked at this post but it doesn't seem to allow for different sets of parameters to be passed into to the two different converters.

-------更新--------

-------Update--------

我也尝试用这个 xaml 创建一个 MultiValueConverter

I also tried to create a MultiValueConverter with this xaml

<Image Grid.Row="4" Grid.Column="0" Source="star_yellow.png">
   <Image.Visibility>
      <MultiBinding Converter="{StaticResource combinedVisibilityConverter}" ConverterParameter="FieldToTest" >
         <Binding ElementName="allowedFieldsModel" Path="Some_Text_Field" Mode="OneWay" />                        
         <Binding ElementName="MyObject" Path="UpdatedFields" Mode="OneWay" />
      </MultiBinding>
   </Image.Visibility>
</Image>

但是当它进入转换器时,两个值都是DependencyProperty.UnsetValue".所以我显然在这里做错了什么.

But when it enters the converter both values are "DependencyProperty.UnsetValue". So I'm apparently doing something wrong here.

--------解决方案---------

--------Solution---------

我不得不对此进行修改,但后来它奏效了.

I had to modify to this, but then it worked.

<Image.Visibility>
    <MultiBinding Converter="{StaticResource combinedVisibilityConverter}" ConverterParameter="FieldToTest">
        <Binding Source="{StaticResource allowedFieldsModel}" Path="Some_Text_Field" />
        <Binding Path="MyObject.UpdatedFields" />
    </MultiBinding>
</Image.Visibility>

推荐答案

您可以将 MultiBinding 与简短的手工 IMultiValueConverter 一起使用.

You could use a MultiBinding together with a short, hand made IMultiValueConverter.

示例:

<StackPanel>
    <StackPanel.Resources>
        <local:MultiBooleanToVisibilityConverter x:Key="Converter" />
    </StackPanel.Resources>
    <CheckBox x:Name="Box1" />
    <CheckBox x:Name="Box2" />
    <TextBlock Text="Hidden Text">
        <TextBlock.Visibility>
            <MultiBinding Converter="{StaticResource Converter}">
                <Binding ElementName="Box1"
                            Path="IsChecked" />
                <Binding ElementName="Box2"
                            Path="IsChecked" />
            </MultiBinding>
        </TextBlock.Visibility>
    </TextBlock>                   
</StackPanel>

...和转换器...

class MultiBooleanToVisibilityConverter : IMultiValueConverter
{
    public object Convert(object[] values,
                            Type targetType,
                            object parameter,
                            System.Globalization.CultureInfo culture)
    {
        bool visible = true;
        foreach (object value in values)
            if (value is bool)
                visible = visible && (bool)value;

        if (visible)
            return System.Windows.Visibility.Visible;
        else
            return System.Windows.Visibility.Hidden;
    }

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

这篇关于如何布尔值 &amp;&amp;两个可见性转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆