布尔值到可见性转换器-反转? [英] Boolean to Visibility Converter - inverted?
问题描述
我该怎么做
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
<WrapPanel>
<TextBlock Text="{Binding ElementName=ConnectionInformation_ServerName,Path=Text}"/>
<Image Source="Images/Icons/Select.ico" Margin="2" Height="15" Visibility="{Binding SQLConnected,Converter={StaticResource BoolToVis},ConverterParameter=true}"/>
<Image Source="Images/Icons/alarm private.ico" Margin="2" Height="15" Visibility="{Binding SQLConnected,Converter={StaticResource BoolToVis},ConverterParameter=false}"/>
</WrapPanel>
是否可以使用布尔值到vis转换器,但无需在C中编写整个方法就可以反转会吗
还是我应该让这些图像重叠并在需要时隐藏它们?
is there a way to use the Boolean to vis converter but inverted without writing a whole method in C to do it? or should i just have these images overlap and hide one when i need to ?
推荐答案
据我所知,您必须为此编写自己的实现。这是我使用的东西:
As far as I know, you have to write your own implementation for this. Here's what I use:
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool boolValue = (bool)value;
boolValue = (parameter != null) ? !boolValue : boolValue;
return boolValue ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我通常设置 ConverterParameter ='negate'
,因此在代码中很清楚参数在做什么。不指定ConverterParameter会使转换器的行为类似于内置的BooleanToVisibilityConverter。如果您希望您的用法能正常工作,则可以使用 bool.TryParse()
解析ConverterParameter并对其做出反应。
And I generally set ConverterParameter='negate'
so it's clear in code what the parameter is doing. Not specifying a ConverterParameter makes the converter behave like the built-in BooleanToVisibilityConverter. If you want your usage to work, you can, of course, parse the ConverterParameter using bool.TryParse()
and react to it.
这篇关于布尔值到可见性转换器-反转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!