绑定转换器中没有逻辑

发布于 2024-12-05 04:44:55 字数 130 浏览 1 评论 0原文

是否可以在不引入像

bool showsearch = !ShowUser;

这样的新变量的情况下执行类似的操作可见性=“{绑定!ShowUser,转换器= {StaticResource BoolToVis}}”

Is it possible to do something similar to this without introducing a new variable like

bool showsearch = !ShowUser;

Visibility="{Binding !ShowUser, Converter={StaticResource BoolToVis}}"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-12-12 04:44:55

您可以使用样式和 DataTrigger 来完成此操作。将样式应用到相关元素,并提供 2 个数据触发值 - 一个将可见性设置为 true,一个设置为 false。类似于 DataTrigger,其中值不为空?

You can do it using a style and a DataTrigger. Apply the style to the element in question, and provide 2 datatrigger values- one to set visibility to true, one to false. Similar to DataTrigger where value is NOT null?

心碎无痕… 2024-12-12 04:44:55

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

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

I don't think it's possible, you have to create an inverted Visibility converter like this,

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;
    }
}
知足的幸福 2024-12-12 04:44:55

您可以编写一个转换器 BoolToCollapsed 转换器 例如

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}/>

You can Write a Converter 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;
             }

}

Add a Static Resource to Window.Resources

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

And then use it to a Grid,DockPanel Or anyother

 <DockPannel Visibility={Binding DockVisible, 
         Converter={StaticResource BoolToCollapsed}/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文