0
我有一個根據兩個屬性啓用的按鈕。我用轉換器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. Why?
任何簡單的解決方案,或者我應該做的邏輯視圖模型和綁定只是爲了一個物業?
CODE: 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;
}
視圖模型變量只是兩個布爾
我編輯,這樣你們可以看到轉換 – Juan
其餘@Juan:編輯我的答案。 –