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

发布于 2024-08-18 21:24:15 字数 255 浏览 5 评论 0原文

好的,这是我的 XAML:

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

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

Ok, so here is my XAML:

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

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

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

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

发布评论

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

评论(5

凝望流年 2024-08-25 21:24:15

您可以使用 ValueConverter:

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

和以下代码隐藏:

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

You could use a 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;
    }
}
黎夕旧梦 2024-08-25 21:24:15

如果您位于(数据)模板内,则可以使用触发器

否则,MVVM-PatternValueConverter 将为您提供帮助。

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

Otherwise, the MVVM-Pattern or a ValueConverter will help you.

绅士风度i 2024-08-25 21:24:15

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

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

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

public class ViewModel
{
   public string InstanceName {...}
   public Visibility InstanceVisibility 
   { 
      get 
      {
           return String.IsNullOrEmpty(InstanceName) ? Visibility.Collapsed : Visibility.Visible;
      }
}
九公里浅绿 2024-08-25 21:24:15
<TextBlock Text="{Binding Path=InstanceName},FallbackValue={x:Null}"></TextBlock>

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

<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.

情话难免假 2024-08-25 21:24:15

好的,这与 PyBinding 很接近:

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

我需要将 IsNotNull 替换为 IsNotNullOrEmpty 的内容,但我越来越接近了。

Ok, so this is close with 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.

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