我有一个根据两个属性启用的按钮。我将 MultiBinding 与转换器一起使用。
一切正常,但输出一直说:
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='' MultiBindingExpression:target element is 'HGCCommandButton' (Name='btnEliminar'); target property is 'IsEnabled' (type 'Boolean')
这里有一些类似问题的问题: WPF MultiBinding Fails。为什么?
任何简单的解决方案,或者我应该在 ViewModel 中创建逻辑并只绑定到一个属性?
代码: XAML:
<utils:HGCCommandButton x:Name="btnEliminar">
<utils:HGCCommandButton.IsEnabled>
<MultiBinding Converter="{StaticResource MultiValueIsEnabledConverter}"
ConverterParameter="NotEnabledIfIsFromInfoGestionOrIsNew">
<Binding Path="IsNew" />
<Binding Path="IsAbonado" />
</MultiBinding>
</utils:HGCCommandButton.IsEnabled>
</utils:HGCCommandButton>
转换器:
public class MultiValueIsEnabledConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (parameter!=null)
{
if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue) return "";
var sel = (MultiValueIsEnabledConverterNames)Enum.Parse(typeof(MultiValueIsEnabledConverterNames), parameter.ToString());
switch (sel)
{
...
case MultiValueIsEnabledConverterNames.NotEnabledIfIsFromInfoGestionOrIsNew:
return (bool)NotEnabledIfIsFromInfoGestionOrIsNew(values[0], values[1]);
default:
throw new ArgumentOutOfRangeException();
}
}
return false;
}
private static object NotEnabledIfIsFromInfoGestionOrIsNew(object isFromIG, object isNew)
{
if ((isFromIG != null) && !(bool)isFromIG)
{
if ((isNew != null) && !(bool)isNew)
{
return !((bool)isFromIG && (bool)isNew);
}
return false;
}
return false;
}
ViewModel 变量只是两个布尔值