绑定转换器中不是逻辑

时间:2011-09-14 23:00:46

标签: xaml data-binding

是否可以在不引入新变量(如

)的情况下执行与此类似的操作

bool showsearch =!ShowUser;

Visibility =“{Binding!ShowUser,Converter = {StaticResource BoolToVis}}”

3 个答案:

答案 0 :(得分:1)

您可以使用样式和DataTrigger来执行此操作。将样式应用于相关元素,并提供2个datatrigger值 - 一个将可见性设置为true,一个设置为false。与DataTrigger where value is NOT null?

类似

答案 1 :(得分:0)

我认为不可能,你必须像这样创建一个倒置的可见性转换器,

public class InvertedBooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool flag = false;
        if (value is bool)
        {
            flag = (bool) value;
        }
        else if (value is bool?)
        {
            bool? nullable = (bool?) value;
            flag = nullable.Value;
        }
        return flag ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value is Visibility && (Visibility) value == Visibility.Collapsed;
    }
}

答案 2 :(得分:0)

您可以编写转换器BoolToCollapsed Converter ForExample

public class BoolToVisibleConverter : ConverterMarkupExtension<BoolToVisibleConverter>, 
              IValueConverter
{
      public object Convert(object value, Type targetType, object parameter, 
                                CultureInfo culture)
            {
      bool obj=(bool) value;
      if(!obj)
      return Visibility.Collapsed;
      else
      return Visibility.Visible;

    }
     public object ConvertBack(object value, Type targetType, object parameter, 
             CultureInfo culture)
             {
               Visibility obj=(Visibility) value;
               if(obj==Visibility.Visible)
               return true;
               else
               return false;
             }

}

向Window.Resources添加静态资源

<Window.Resources>
    <converter:BoolToVisibleConverter x:Key="BoolToCollapsed"/>
</Window.Resources>

然后将它用于Grid,DockPanel或任何其他

 <DockPannel Visibility={Binding DockVisible, 
         Converter={StaticResource BoolToCollapsed}/>