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

后端 未结 3 1449
闹比i
闹比i 2020-12-11 00:10

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

相关标签:
3条回答
  • 2020-12-11 00:15

    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!

    0 讨论(0)
  • 2020-12-11 00:27

    Just in addition to all other answers, I usually add these lines to the beginning of Convert method:

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Any(x => x == DependencyProperty.UnsetValue))
                return DependencyProperty.UnsetValue;
    
            ...
    
        }
    

    to make sure that none of the values is unset (that usually happens with DataGrid with CanUserAddRows="True").

    0 讨论(0)
  • 2020-12-11 00:36

    If occuring in a datagrid try setting CanUserAddRows="False"

    0 讨论(0)
提交回复
热议问题