I have a button that is enabled depending on two properties. I used a MultiBinding with a Converter.
Everything works but the output keeps saying:
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')
There are some questions here with similar issues: WPF MultiBinding Fails. Why?
Any easy solution or I should make the logic in the ViewModel and bind just to one property?
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>
Converter:
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 variables are just two booleans
That is quite an odd ConverterParameter
, is the StaticResource
you reference for the Converter
actually an instance of the converter whose code you posted? The error claims that the value is not a bool and that method alone can only return a bool so i doubt that the error is found here.
Can the code in your Convert
method return anything else but a bool? You need to avoid it if that is possible.
Edit: Right here is the problem:
if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue) return "";
You return an empty string which is not compatible with the boolean property,