Why do I get a DependencyProperty.UnsetValue when converting a value in a MultiBinding?

隐身守侯 提交于 2020-01-10 03:28:12

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/2811405/why-do-i-get-a-dependencyproperty-unsetvalue-when-converting-a-value-in-a-multib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!