2つのプロパティに応じて有効になるボタンがあります。コンバーターでマルチバインディングを使用しました。
すべてが機能しますが、出力は次のように言い続けます。
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マルチバインディングが失敗します。なんで?
簡単な解決策はありますか、それともViewModelでロジックを作成し、1つのプロパティにバインドする必要がありますか?
コード: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変数は2つのブール値です