布尔到可见性转换器

New*_*Bee 0 c# wpf converter

我该怎么做这样的事情

<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>
Run Code Online (Sandbox Code Playgroud)

有没有办法使用布尔到vis转换器但倒转而不用在C中编写整个方法来做到这一点?或者我应该让这些图像重叠并在需要时隐藏一个?

K M*_*hta 6

据我所知,你必须为此编写自己的实现.这是我使用的:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我通常会ConverterParameter='negate'在代码中明确指出参数是做什么的.不指定ConverterParameter使转换器的行为类似于内置的BooleanToVisibilityConverter.如果您希望您的使用工作,您当然可以解析ConverterParameter bool.TryParse()并对其做出反应.