如果值为0,如何隐藏控件?

时间:2013-01-21 13:12:28

标签: xaml windows-8 windows-runtime winrt-xaml

在xaml中,可以使用DataTemplate.Triger处理控件的外观,但在win8 xaml中它不可用:

<DataTrigger Binding="{Binding Role.Count, Converter={StaticResource isGreaterThanZero}" Value="True">
    <Setter TargetName="Counts" Property="Visibility" Value="Hidden" />
</DataTrigger>

如何在Windows 8 xaml中执行相同的操作?

1 个答案:

答案 0 :(得分:0)

如果查看网格和拆分布局的默认Visual Studio模板,您将找到一个值转换器的示例,该转换器基于布尔值设置控件可见性。代码显示在这里

public sealed class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value is Visibility && (Visibility)value == Visibility.Visible;
        }
    }

您需要复制此代码并修改它以根据整数值设置visibility属性 - 例如IntToVisibilityConverter。您可以在想要受影响的控件上使用这样的转换器

Visibility="{Binding IsActive, Converter={StaticResource IntToVisibilityConverter}}"