wpf 如何隐藏空的 TextBlock?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5573864/
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 22:46:39  来源:igfitidea点击:

How to hide the empty TextBlock?

wpf

提问by Boris

In the XAML provided below, I don't have the value for Phone sometimes. When that happens, the value is missing, but the TextBlockis still occupying the space in the panel. I want to hide empty TextBlocks from taking space in the StackPanel.

在下面提供的 XAML 中,有时我没有 Phone 的值。发生这种情况时,该值丢失,但TextBlock仍占据面板中的空间。我想隐藏空TextBlocks 以免占用StackPanel.

Here's the XAML:

这是 XAML:

<StackPanel>
    <TextBlock Text="{Binding Path=FirstName}" />
    <TextBlock Text="{Binding Path=LastName}" />
    <TextBlock Text="{Binding Path=Phone}" />
    <TextBlock Text="{Binding Path=Email}" />
</StackPanel>

I've read this article, but the accepted answer doesn't work for me:

我已经阅读了这篇文章,但接受的答案对我不起作用:

<StackPanel>
    <TextBlock Text="{Binding Path=FirstName}" />
    <TextBlock Text="{Binding Path=LastName}" />
    <TextBlock Text="{Binding Path=Phone}">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <Trigger Property="Text" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Collapsed" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    <TextBlock Text="{Binding Path=Email}" />
</StackPanel>

Am I making a mistake somewhere, or is the accepted answer wrong? What should I do to achieve my goal?

我是在某个地方犯了错误,还是接受的答案是错误的?我应该怎么做才能实现我的目标?

回答by CodeNaked

You probably need to use:

您可能需要使用:

<Style TargetType="TextBlock">
        <Style.Triggers>
            <Trigger Property="Text" Value="">
                <Setter Property="Visibility" Value="Collapsed" />
            </Trigger>
        </Style.Triggers>
</Style>

or maybe both:

或者两者兼而有之:

<Style TargetType="TextBlock">
        <Style.Triggers>
            <Trigger Property="Text" Value="">
                <Setter Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="Text" Value="{x:Null}">
                <Setter Property="Visibility" Value="Collapsed" />
            </Trigger>
        </Style.Triggers>
</Style>

回答by Brad Cunningham

Instead of introducing a style just for this, for this sort of thing I tend to prefer using a converter that will handle a null or empty string.

而不是为此引入样式,对于这类事情,我倾向于使用一个转换器来处理 null 或空字符串。

<TextBlock Text="{Binding Foo}"
           Visibility="{Binding Foo, 
                        Converter={StaticResource StringToVisibilityConverter}}" />

Where StringToVisibilityConverteris defined like this:

whereStringToVisibilityConverter是这样定义的:

[ValueConversion(typeof(string), typeof(Visibility))]
public class StringToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (string.IsNullOrEmpty((string)value))
        {
            return Visibility.Collapsed;
        }
        else
        {
            return Visibility.Visible;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

回答by devdigital

You can use a DataTrigger:

您可以使用DataTrigger

<TextBlock Text="{Binding Path=Title}">
    <TextBlock.Style>
       <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Title}" Value="{x:Null}">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
       </Style>
    </TextBlock.Style>
</TextBlock>