【发布时间】:2010-05-28 03:16:20
【问题描述】:
我想将控件可见性绑定到布尔属性值的反转。我有一个属性 CanDownload,如果它是真的,那么我想隐藏文本框,反之亦然。我怎样才能做到这一点?
谢谢
【问题讨论】:
标签: silverlight silverlight-3.0
我想将控件可见性绑定到布尔属性值的反转。我有一个属性 CanDownload,如果它是真的,那么我想隐藏文本框,反之亦然。我怎样才能做到这一点?
谢谢
【问题讨论】:
标签: silverlight silverlight-3.0
这类问题如此经常被问到,答案如此相似 我认为是时候对所有(好吧可能是“大多数”)布尔值转换问题。我已经在博客上写了here。
代码很简单,所以我也贴在这里:-
public class BoolToValueConverter<T> : IValueConverter
{
public T FalseValue { get; set; }
public T TrueValue { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return FalseValue;
else
return (bool)value ? TrueValue : FalseValue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value != null ? value.Equals(TrueValue) : false;
}
}
现在您可以使用单线创建转换器到可见性:-
public class BoolToVisibilityConverter : BoolToValueConverter<Visibility> { }
然后你可以在这样的资源中创建一个实例转换器:-
<local:BoolToVisibilityConverter x:Key="InverseVisibility" TrueValue="Collapsed" FalseValue="Visible" />
请注意,TrueValue 和 FalseValue 是从更自然的顺序交换而来,为您提供所需的反转逻辑。在 Resources 中的 UserControl 甚至 App.xaml 中使用它,您现在可以使用它来绑定到 CanDownload 属性到 TextBox Visibility 属性:-
<TextBox Visibility="{Binding CanDownload, Converter={StaticResource InverseVisibility}}" />
【讨论】:
targetType 参数的类型为System.Type,在这种情况下,它当前的值为System.Windows.Visibility。 (再次仔细阅读,这是一个“代数”时刻,在硬币下降之前,其他任何事情都没有意义。)当你做targetType.GetType()时,你现在有一个Type,它的值是Type,在这种情况下,System.RuntimeType 派生自 System.Type。如果最后一句话没有意义,那是因为您还没有掌握我的第一句话。见:-msdn.microsoft.com/en-us/library/system.type(v=VS.100).aspx
我使用 BoolToVisibilityConverter,它允许您将“Inverse”作为 ConverterParameter 传递以反转转换并仅在属性为 false 时显示。
public class BoolToVisibilityConverter : IValueConverter
{
/// <exception cref="ArgumentException">TargetType must be Visibility</exception>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(!(value is bool))
throw new ArgumentException("Source must be of type bool");
if(targetType != typeof(Visibility))
throw new ArgumentException("TargetType must be Visibility");
bool v = (bool) value;
if(parameter is string && parameter.ToString() == "Inverse")
v = !v;
if (v)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
【讨论】:
我能够使用布尔到可见性转换器为最近的项目解决这个问题:
public class BoolToVisibilityConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.GetType().Equals(typeof(bool)))
{
if ((bool)value == true)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
else
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.GetType().Equals(typeof(Visibility)))
{
if ((Visibility)value == Visibility.Collapsed)
return false;
else
return true;
}
else
return false;
}
#endregion
}
我想我可能已经替换了这一行:
if (value.GetType().Equals(typeof(Visibility)))
像这样更简单的东西:
if (value is Visibility)
与 bool GetType 相同,但你明白了。
当然,在您的转换器中,您可以根据您的可见性需求反转可见性值。希望这会有所帮助。
【讨论】: