WPF BooleanToVisibilityConverter that converts to Hidden instead of Collapsed when false?

后端 未结 6 1946
予麋鹿
予麋鹿 2020-12-12 22:14

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 writ

6条回答
  •  春和景丽
    2020-12-12 22:46

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

提交回复
热议问题