如果您查看网格和拆分布局的默认 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;
}
}
您需要做的就是复制此代码并对其进行修改以根据整数值设置可见性属性 - 例如,IntToVisibilityConverter。你可以在你想受到影响的控件上使用这样的转换器
Visibility="{Binding IsActive, Converter={StaticResource IntToVisibilityConverter}}"