如何进行布尔运算 &&两个可见度转换器
我有两个单独的可见性转换器,一个基于字段是否已更新,另一个基于是否允许查看字段。我对页面上的每个文本项使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将 MultiBinding 与简短的手工制作的 IMultiValueConverter 一起使用。
示例:
...以及转换器...
You could use a MultiBinding together with a short, hand made IMultiValueConverter.
Example:
... and the converter ...
虽然已经晚了,但一个更简单的解决方案是将控件包装在另一个控件中。我更喜欢这样而不是有很多做不同事情的转换器。
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.
我想到的一件事也许是,不是两个不同的布尔字段,而是通过 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).
您可以将两个对象的数组传递给
ConverterParameter
中的转换器 - 在 XAML 中构造该数组。You could pass an array of two objects to the converter in the
ConverterParameter
- constructing the array in XAML.