输出中的多重绑定错误
我有一个根据两个属性启用的按钮。我使用了带有转换器的多重绑定。
一切正常,但输出一直显示:
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 中创建逻辑并仅绑定到一个属性?
代码: 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 变量只是两个布尔值
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个相当奇怪的
ConverterParameter
,您为Converter
引用的StaticResource
实际上是您发布其代码的转换器的实例吗?该错误声称该值不是布尔值,并且该方法单独只能返回布尔值,因此我怀疑在这里找到了错误。Convert
方法中的代码可以返回除 bool 之外的其他值吗?如果可能的话,您需要避免它。编辑:问题就在这里:
您返回一个与布尔属性不兼容的空字符串,
That is quite an odd
ConverterParameter
, is theStaticResource
you reference for theConverter
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:
You return an empty string which is not compatible with the boolean property,