WPF:通过字符串内容绑定可见性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2122780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:27:13  来源:igfitidea点击:

WPF: Binding Visibility by string contents

wpfbindingvisibilitytextblock

提问by Jonathan Allen

Ok, so here is my XAML:

好的,这是我的 XAML:

<TextBlock Text="{Binding Path=InstanceName}"></TextBlock>

If InstanceNameis null or an empty string, I want Visibility="Collapsed". Otherwise I want Visibility="Visible". How would I do that?

如果InstanceName为 null 或空字符串,我想要Visibility="Collapsed". 否则我要Visibility="Visible"。我该怎么做?

回答by Markus Hütter

You could use a ValueConverter:

您可以使用 ValueConverter:

<TextBlock 
     Visibility="{Binding InstanceName, Converter={local:StringNullOrEmptyToVisibilityConverter}}" 
     Text="{Binding InstanceName}"/>

with the following codebehind:

带有以下代码隐藏:

public class StringNullOrEmptyToVisibilityConverter : System.Windows.Markup.MarkupExtension, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return string.IsNullOrEmpty(value as string) 
            ? Visibility.Collapsed : Visibility.Visible;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
    public override object ProvideValue(IServiceProvider serviceProvider)
    {            
        return this;
    }
}

回答by Matthias

If you are inside a (Data-)Template you can use Triggersfor that.

如果您在 (Data-)Template 中,您可以使用触发器

Otherwise, the MVVM-Patternor a ValueConverterwill help you.

否则,MVVM-PatternValueConverter将帮助您。

回答by Peter Lillevold

By putting an extra property in your viewmodel that you can bind the Visibility attribute to:

通过在您的视图模型中放置一个额外的属性,您可以将 Visibility 属性绑定到:

public class ViewModel
{
   public string InstanceName {...}
   public Visibility InstanceVisibility 
   { 
      get 
      {
           return String.IsNullOrEmpty(InstanceName) ? Visibility.Collapsed : Visibility.Visible;
      }
}

回答by Kishore Kumar

<TextBlock Text="{Binding Path=InstanceName},FallbackValue={x:Null}"></TextBlock>

Then add a DataTrigger to check the value is null and change visibility using Setter. This is the simple method which iam using.

然后添加一个 DataTrigger 来检查值是否为 null 并使用 Setter 更改可见性。这是我使用的简单方法。

回答by Jonathan Allen

Ok, so this is close with PyBinding:

好的,这与 PyBinding 很接近:

<TextBlock Text="{Binding Path=InstanceName}" Visibility="{p:PyBinding BooleanToVisibility(IsNotNull($[.InstanceName]))}"  ></TextBlock>

I need to replace IsNotNull with something that means IsNotNullOrEmpty, but I'm getting closer.

我需要用意味着 IsNotNullOrEmpty 的东西替换 IsNotNull,但我越来越接近了。