如何进行布尔运算 &&两个可见度转换器

发布于 2024-11-17 23:02:09 字数 1957 浏览 2 评论 0原文

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

例如:

<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}}" />

,对于需要权限的字段,我需要运行两个转换器来确定星星是否出现。有没有办法对两个转换器的结果进行布尔“与”?

我查看了这篇文章,但它没有似乎允许将不同的参数集传递到两个不同的转换器。

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

我还尝试使用此 xaml 创建一个 MultiValueConverter

<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”。所以我显然在这里做错了什么。

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

我必须对此进行修改,但随后它就起作用了。

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

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.

For example:

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

and

<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--------

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

維他命╮ 2024-11-24 23:02:09

您可以将 MultiBinding 与简短的手工制作的 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();
    }
}

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

Example:

<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>

... and the converter ...

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();
    }
}
终止放荡 2024-11-24 23:02:09

虽然已经晚了,但一个更简单的解决方案是将控件包装在另一个控件中。我更喜欢这样而不是有很多做不同事情的转换器。

<Border Visibility="{Binding Value1, Converter={convertersDF:Converter_ValueToVisibility}}">
 <ComboBox Visibility="{Binding Value2, Converter={convertersDF:Converter_ValueToVisibility}}"/>
</Border>

Late to the party here but an easier solution is to just wrap the control in another control. I prefer this to having lots of Converters that do different things.

<Border Visibility="{Binding Value1, Converter={convertersDF:Converter_ValueToVisibility}}">
 <ComboBox Visibility="{Binding Value2, Converter={convertersDF:Converter_ValueToVisibility}}"/>
</Border>
娇女薄笑 2024-11-24 23:02:09

我想到的一件事也许是,不是两个不同的布尔字段,而是通过 ORing 一起更新的字段和 allowedField 创建的单个位字段。然后您可以拥有三个值转换器,它们都在同一字段上运行。

或者只是计算数据模型中进行 AND 运算的另一个字段。这可能更有效(就运行时间而言)。

One thing that came to mind is, perhaps, instead of two different boolean fields, a single bit field created by ORing together updatedField and allowedField. Then you can have three value converters, all operating on the same field.

Or just calculate another field in your data model that does the ANDing there. That's probably more efficient (in terms of runtime).

冰葑 2024-11-24 23:02:09

您可以将两个对象的数组传递给 ConverterParameter 中的转换器 - 在 XAML 中构造该数组。

You could pass an array of two objects to the converter in the ConverterParameter - constructing the array in XAML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文