代码之家  ›  专栏  ›  技术社区  ›  Rich

WPF BooleanToVisibilityConverter,当为false时转换为隐藏而不是折叠?

  •  44
  • Rich  · 技术社区  · 15 年前

    5 回复  |  直到 15 年前
        1
  •  31
  •   Adam    8 年前

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

    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);
    }
    
        2
  •  130
  •   Drew Noakes    14 年前

    [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;
      }
    }
    

    使用它时,只需配置一个在XAML中完全满足您需要的版本,如下所示:

    <Blah.Resources>
      <local:BoolToVisibilityConverter
             x:Key="BoolToHiddenConverter"
             TrueValue="Visible" FalseValue="Hidden" />
    </Blah.Resources>
    

    然后在一个或多个绑定中使用它,如下所示:

    <Foo Visibility="{Binding IsItFridayAlready, 
                              Converter={StaticResource BoolToHiddenConverter}}" />
    

    SILVERLIGHT用户 我必须放下枪 [ValueConversion] 作为该属性的声明不是Silverlight框架的一部分。它在WPF中也不是严格需要的,但与内置转换器是一致的。

        3
  •  4
  •   cristobalito    15 年前

    style 而不是转换器?代码类似于:

    <Style x:Key="Triggers" TargetType="Button">
        <Style.Triggers>
        <Trigger Property="{Binding ...}" Value="false">
            <Setter Property = "Visibility" Value="Hidden"/>
        </Trigger>
        </Style.Triggers>
    </Style>
    

    您需要自己提供绑定属性来指向bool属性。

        4
  •  4
  •   hkon    15 年前

    要反转逻辑,只需在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);
    }
    
        5
  •  1
  •   FredM d.moncada    7 年前

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var boolValue = (bool) value;
        return boolValue ? Visibility.Visible : (parameter ?? Visibility.Hidden);
    }
    

    所以你可以这样绑定:

    Visibility="{Binding SomeBool, Converter={StaticResource ResourceKey=BooleanToVisibilityConverter}, ConverterParameter={x:Static Visibility.Collapsed}}"
    

        6
  •  0
  •   Steven Albright    6 年前

    我有这个问题突然出现,我的解决方案可能是非常情境,但我会分享它无论如何。 由于我的环境,我能够模仿转换器,没有转换器,用一个简单的代码位。只有当绑定到textbox生成的数字框的变量(通过Regex确保其a数字)不是0时,我才改变可见性。整个代码都在下面,但是如果要在代码的其他地方更改布尔值,那么WPF和C的第一位才是真正需要的。 工作包:

        Visibility="{Binding Path=Visible}"
    

    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));
            }
        }
    
        7
  •  0
  •   Michael Murphy    5 年前

    在我看来,我有一个按钮,我想被隐藏,除非我的变量之一有一个正长度字符串:

    <Button x:Name="SelectBinaryFilePath" Content="Select" Visibility="{Binding CanSelectBinaryFile}" />
    

    public Visibility CanSelectBinaryFile
    {
        get
        {
            return String.IsNullOrEmpty(FileSystemPath) ? Visibility.Hidden : Visibility.Visible;
        }
    }
    

    NotifyOfPropertyChange 以确保 CanSelectBinaryFile

    推荐文章