WPF BooleanToVisibilityConverter要转换为隐藏,而不是折叠时假的?转换为、而不是、WPF、BooleanToVisibilityConverter

2023-09-04 01:35:12 作者:如梦如幻

有没有一种方法使用现有的WPF BooleanToVisibilityConverter转换器,但有假值转换为隐藏,而不是崩溃了默认值,或应我只是写我自己?我在一个项目中,这是巨大的开销,像这样简单的东西(共享的东西进入一个单独的解决方案,并重建/签/合并的过程是一个过程的一个杂草丛生的变异巨兽),所以我倒是preFER如果我能一个参数仅仅传递给现有的,而不是通过跳箍刚才提到的。

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.

推荐答案

不幸的是,它只能转换为可见或折叠,所以你必须写自己的。下面是根据反射转换方法:

Unfortunately, it only converts to Visible or Collapsed, so you'll have to write your own. Here is the Convert method according to Reflector:

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.Hidden);
}