.net WPF BooleanToVisibilityConverter 在 false 时转换为隐藏而不是折叠?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3128023/
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
WPF BooleanToVisibilityConverter that converts to Hidden instead of Collapsed when false?
提问by Rich
Is there a way to use the existing WPF BooleanToVisibilityConverter converter but have False values convert to Hidden instead of the default Collapsed, or should I just write my own? I'm on a project where it's tremendous overhead to do something simple like this (shared stuff goes into a separate solution, and the rebuild/checkin/merge process is an overgrown mutated behemoth of a process), so I'd prefer if I could just pass a parameter to the existing one than to jump through the hoops just mentioned.
有没有办法使用现有的 WPF BooleanToVisibilityConverter 转换器,但将 False 值转换为 Hidden 而不是默认的 Collapsed,还是我应该自己编写?我在一个项目中,做这样简单的事情会产生巨大的开销(共享的东西进入一个单独的解决方案,重建/签入/合并过程是一个过度生长的变异过程的庞然大物),所以我更喜欢如果我可以只将一个参数传递给现有的参数,而不是跳过刚才提到的箍。
采纳答案by Quartermeister
Unfortunately, it only converts to Visible or Collapsed, so you'll have to write your own. Here is the Convert method according to Reflector:
不幸的是,它只能转换为 Visible 或 Collapsed,因此您必须自己编写。这是根据 Reflector 的 Convert 方法:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool flag = false;
if (value is bool)
{
flag = (bool)value;
}
else if (value is bool?)
{
bool? nullable = (bool?)value;
flag = nullable.HasValue ? nullable.Value : false;
}
return (flag ? Visibility.Visible : Visibility.Collapsed);
}
回答by Drew Noakes
I've found the simplest and best solution to be this:
我发现最简单和最好的解决方案是这样的:
[ValueConversion(typeof(bool), typeof(Visibility))]
public sealed class BoolToVisibilityConverter : IValueConverter
{
public Visibility TrueValue { get; set; }
public Visibility FalseValue { get; set; }
public BoolToVisibilityConverter()
{
// set defaults
TrueValue = Visibility.Visible;
FalseValue = Visibility.Collapsed;
}
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (!(value is bool))
return null;
return (bool)value ? TrueValue : FalseValue;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (Equals(value, TrueValue))
return true;
if (Equals(value, FalseValue))
return false;
return null;
}
}
When using it, just configure a version that does exactly what you need it to in XAML like this:
使用它时,只需在 XAML 中配置一个完全符合您需要的版本,如下所示:
<Blah.Resources>
<local:BoolToVisibilityConverter
x:Key="BoolToHiddenConverter"
TrueValue="Visible" FalseValue="Hidden" />
</Blah.Resources>
Then use it in one or more bindings like this:
然后在一个或多个绑定中使用它,如下所示:
<Foo Visibility="{Binding IsItFridayAlready,
Converter={StaticResource BoolToHiddenConverter}}" />
This simple solution addresses hidden/collapsed preferences as well as reversing/negating the effect.
这个简单的解决方案解决了隐藏/折叠的偏好以及逆转/否定效果。
SILVERLIGHT USERSmust drop the [ValueConversion]declaration as that attribute is not part of the Silverlight framework. It's not strictly needed in WPF either, but is consistent with built-in converters.
SILVERLIGHT 用户必须删除[ValueConversion]声明,因为该属性不是 Silverlight 框架的一部分。在 WPF 中也不是严格需要的,但与内置转换器一致。
回答by cristobalito
Can you not just use a styleinstead of a converter? The code would be something like:
您不能只使用样式而不是转换器吗?代码类似于:
<Style x:Key="Triggers" TargetType="Button">
<Style.Triggers>
<Trigger Property="{Binding ...}" Value="false">
<Setter Property = "Visibility" Value="Hidden"/>
</Trigger>
</Style.Triggers>
</Style>
You'll need to provide the property binding yourself to point to your bool property.
您需要提供绑定自己的属性以指向您的 bool 属性。
回答by hkon
I like to use the parameter for reversing the visibility logic: To reverse logic simply put: ConverterParameter=Reverse in your xaml code
我喜欢使用参数来反转可见性逻辑:要反转逻辑,只需在您的 xaml 代码中输入: ConverterParameter=Reverse
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool flag = false;
if (value is bool)
{
flag = (bool)value;
}
var reverse = parameter as string;
if(reverse != null && reverse == "Reverse")
flag != flag;
return (flag ? Visibility.Visible : Visibility.Collapsed);
}
回答by Krzysztof Skowronek
I wrote BoolToVisibilityConverte where you can pass invisible state in Parameter:
我写了 BoolToVisibilityConverte,您可以在其中传递参数中的不可见状态:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var boolValue = (bool) value;
return boolValue ? Visibility.Visible : (parameter ?? Visibility.Hidden);
}
So you can bind like this:
所以你可以像这样绑定:
Visibility="{Binding SomeBool, Converter={StaticResource ResourceKey=BooleanToVisibilityConverter}, ConverterParameter={x:Static Visibility.Collapsed}}"
Hope this helps :)
希望这可以帮助 :)
回答by Steven Albright
I had this issue crop up and my solution was probably very situational but I'll share it anyway. Due to my circumstance I was able to mimic the converter, without a converter, with a simple bit of code. I had visibility change only if a variable bound to a textbox made number box( through a Regex to ensure its a number) was not 0. The entire code for this is below however the WPF and first bit of C# is all you really need if you are going to change your boolean elsewhere in code. wpf:
我突然遇到了这个问题,我的解决方案可能非常特殊,但无论如何我都会分享。由于我的情况,我能够在没有转换器的情况下用一些简单的代码模拟转换器。仅当绑定到文本框的变量使数字框(通过正则表达式确保其数字)不为 0 时,我才能更改可见性。 整个代码如下,但是 WPF 和 C# 的第一位是你真正需要的,如果您将在代码中的其他地方更改您的布尔值。wpf:
Visibility="{Binding Path=Visible}"
C#
C#
public class foo : INotifyPropertyChanged
{
private bool vis = false;
public object Visible
{
get
{
if (vis == true)
{
return Visibility.Visible;
}
else
{
return Visibility.Hidden;
}
}
set
{
vis = (bool)value;
OnPropertyChanged(nameof(Visible));
}
}
public int Value_b
{
get
{
return base_value;
}
set
{
base_value = value;
OnPropertyChanged(nameof(Value_b));
if (base_value == 0)
{
Visible = false;
}
else
{
Visible = true;
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
}

