有一种情况,UI中的复选框需要绑定到数字参数 - 有效地,某些值使复选框为“true”,否则为“false”。WPF - ConverterParameter中的动态值
做到这一点最简单的方法似乎是使用转换器:
[ValueConversion(typeof(int), typeof(bool?))]
public class TypeToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool?))
throw new InvalidOperationException("The target must be a bool");
if((value < 3)
{
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplmentedExpection();
}
}
然后在XAML:
<CheckBox IsChecked="{Binding Path=Type.TypeID, Converter={StaticResource TypeConverter}}" />
使用转换时就像一个魅力哪些工作,但完全失败时使用ConvertBack是因为它需要知道数值是什么(它取决于其他UI元素),然后才能知道要返回的数字 - 它需要访问绑定的对象。
我认为我可以用ConverterParameter做到这一点,但从外观上看,您无法将值绑定到此属性。
有没有办法摆脱这个混乱?
编辑:我解决了这个混乱的原始绑定,并逃避了它,因为取消所有我想要做的是删除该项目无论如何。但是我会留下这个地方,因为这似乎是一个有效的问题,我对可能的解决方案感到好奇。
这可以有任何帮助吗? http://stackoverflow.com/questions/397556/how-to-bind-radiobuttons-to-an-enum – Lucian