wpf 如何布尔值&& 两个可见性转换器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6501970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to boolean && two visibility converters
提问by Bill
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.
我有两个单独的可见性转换器,一个基于字段是否已更新,另一个基于是否允许查看字段。我对页面上的每个文本项使用了 updatedField ,以便在更新的字段旁边显示一颗星。但某些文本项仅对某些用户可见,具体取决于权限级别。
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?
我的问题是,对于需要许可的字段,我需要运行两个转换器来确定星星是否出现。有没有办法对两个转换器的结果执行布尔值“And”?
I looked at this postbut 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
我还尝试用这个 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>
But when it enters the converter both values are "DependencyProperty.UnsetValue". So I'm apparently doing something wrong here.
但是当它进入转换器时,两个值都是“DependencyProperty.UnsetValue”。所以我显然在这里做错了什么。
--------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>
回答by Jens
You could use a MultiBinding together with a short, hand made IMultiValueConverter.
您可以将 MultiBinding 与简短的手工 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();
}
}
回答by Asheh
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>
回答by i_am_jorf
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.
想到的一件事是,也许不是两个不同的布尔字段,而是通过 ORing 将 updatedField 和 allowedField 组合在一起创建的单个位字段。然后,您可以拥有三个值转换器,它们都在同一字段上运行。
Or just calculate another field in your data model that does the ANDing there. That's probably more efficient (in terms of runtime).
或者只是计算数据模型中的另一个字段,在那里进行 ANDing。这可能更有效(就运行时而言)。
回答by Kieren Johnstone
You could pass an array of two objects to the converter in the ConverterParameter
- constructing the array in XAML.
您可以将包含两个对象的数组传递给转换器ConverterParameter
- 在 XAML 中构造数组。