为什么在MultiBinding中转换值时会得到DependencyProperty.UnsetValue? [英] Why do I get a DependencyProperty.UnsetValue when converting a value in a MultiBinding?

查看:75
本文介绍了为什么在MultiBinding中转换值时会得到DependencyProperty.UnsetValue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的IMultiValueConverter,可以对两个值进行或运算。在下面的示例中,我想使用一个同样简单的布尔值反相器来反转第一个值。

 < MultiBinding Converter = { StaticResource multiBoolToVis}> 
< Binding Path = ConditionA Converter = {StaticResource boolInverter} />
< Binding Path = ConditionB />
< / MultiBinding>

和逆变器:

 公共类BoolInverterConverter:IValueConverter 
{
#region IValueConverter成员
公共对象Convert(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化)
{
if(值是布尔值)
{
return!((bool)value);
}
返回null;
}
公共对象ConvertBack(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化)
{
throw new NotImplementedException();
}
#endregion
}

当我包含boolInverter时,MultiValueConverter中的第一个值将成为 DependencyProperty.UnsetValue。当我不使用转换器时,没有任何问题(当然,除了我要使用的逻辑之外)。



我错过了什么吗?单步调试器显示InverseBoolConverter正确反转了我传递的值,但随后该值未发送到MultiValueConverter。

解决方案

从MSDN:



UnsetValue是一个哨兵值,用于WPF属性系统无法确定请求的DependencyProperty值的情况。使用UnsetValue而不是使用null引用(在Visual Basic中为Nothing),因为null引用可以是有效的属性值,也可以是有效的(并且经常使用的)DefaultValue。



这意味着以下内容之一:




  • 您使用模板( ControlTemplate DataTemplate ),并且该值在加载时未设置数据源。因此它将两次对您的转换器产生影响,首先是UnsetValue,其次是布尔值;因此不必担心;

  • 您的 Binding 是错误的,这意味着Binding无法确定值,从而导致UnsetValue。



此外,您不能像您那样合并Converters。 >

在内部绑定中删除转换器,它应该是固定的! :)



希望这会有所帮助!


I have an extremely simple IMultiValueConverter that simply OR's two values. In the example below, I want to invert the first value using an equally simple boolean inverter.

<MultiBinding Converter="{StaticResource multiBoolToVis}">
    <Binding Path="ConditionA" Converter="{StaticResource boolInverter}"/>
    <Binding Path="ConditionB"/>
</MultiBinding>

and the inverter:

public class BoolInverterConverter : IValueConverter
{
    #region IValueConverter Members
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool)
        {
            return !((bool)value);
        }
        return null;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
    #endregion
}

When I include the boolInverter, the first value in the MultiValueConverter becomes a "DependencyProperty.UnsetValue". There are no problems when I do not use the converter (other than not the logic I am aiming for, of course).

Am I missing something? Stepping through the debugger shows that the InverseBoolConverter is properly inverting the value I pass it, but that value is then not being 'sent' to the MultiValueConverter.

解决方案

From MSDN:

UnsetValue is a sentinel value that is used for scenarios where the WPF property system is unable to determine a requested DependencyProperty value. UnsetValue is used rather than null reference (Nothing in Visual Basic), because null reference could be a valid property value, as well as a valid (and frequently used) DefaultValue.

Which means one of the following things:

  • You use a template (ControlTemplate or DataTemplate), and the value does not have a DataSource set at the time of being Loaded. So it will hit your converter twice, first with the UnsetValue, second with the boolean value; so nothing to worry about;
  • Your Binding is incorrect, meaning the Binding cannot determine a value, thus resulting in the UnsetValue.. You should propbably see a warning..

Also, you cannot combine Converters like you do.. So its probably that.

Remove the Converter in the inner Binding, and it should be fixed! :)

Hope this helps!

这篇关于为什么在MultiBinding中转换值时会得到DependencyProperty.UnsetValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆