使用样式有条件地隐藏 WinRT/XAML 控件 - 可能吗?

Conditional Hiding of WinRT/XAML Controls Using Style - Possible?

在我的 WinRT/Phone 8.1 应用程序中,我有一个表单,其中包含许多 Grids(用作包装器),每个包含两个或更多 TextBlocks。我只想显示可用的数据,这意味着如果特定 Grid 的内容 TextBlock 为空,我想隐藏整个 Grid.

例如:´

<Grid x:Name="NameSection">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0"
               x:Name="NameLabel"
               Text="Name:" />
    <TextBlock Grid.Row="1"
               x:Name="Name"
               Text="{Binding Name}" />
</Grid>

如果 Name TextBlock 为空,则应折叠整个 Grid 的可见性。

为此添加逻辑以隐藏代码或(更糟糕的是)ViewModel 可能会使这种长格式变得混乱,所以我想知道我是否可以使用 XAML 和样式来实现此目的。如何在 WinRT 中完成?我可以设置 Grid 的样式,使其可见性基于其子视图之一中的内容吗?

转换器

public class NullToVisibilityConverter: IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value == null ? Visibility.Collapsed: Visibility.Visible;
        }

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

然后像这样使用它

<Grid x:Name="NameSection" Visibility={Binding Name, Converter={StaticResource MyNullConverter}}>

编辑: 如果您还想检查空字符串,可以添加 string.IsNullOrEmpty(value as string) insted of value == null