Why does my data binding see the real value instead of the coerced value?

后端 未结 3 1813
轻奢々
轻奢々 2020-12-09 18:21

I\'m writing a real NumericUpDown/Spinner control as an exercise to learn custom control authoring. I\'ve got most of the behavior that I\'m looking for, inclu

相关标签:
3条回答
  • 2020-12-09 18:41

    A new answer for an old question: :-)

    In the registration of the ValueProperty an FrameworkPropertyMetadata instance is used. Set the UpdateSourceTrigger property of this instance to Explicit. This can be done in a constructor overload.

    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(int), typeof(NumericUpDown), 
        new FrameworkPropertyMetadata(
          0, 
          FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, 
          HandleValueChanged, 
          HandleCoerceValue,
          false
          UpdateSourceTrigger.Explicit));
    

    Now the binding source of the ValueProperty will not be updated automatically on PropertyChanged. Do the update manually in your HandleValueChanged method (see code above). This method is called only on 'real' changes of the property AFTER the coerce method has been called.

    You can do it this way:

    static void HandleValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        NumericUpDown nud = obj as NumericUpDown;
        if (nud == null)
            return;
    
        BindingExpression be = nud.GetBindingExpression(NumericUpDown.ValueProperty);
        if(be != null)
            be.UpdateSource();
    }
    

    In this way you can avoid to update your bindings with non-coerced values of your DependencyProperty.

    0 讨论(0)
  • 2020-12-09 18:43

    You are coercing v which is an int and as such a value type. It is therefore stored on the stack. It is in no way connected to baseValue. So changing v will not change baseValue.

    The same logic applies to baseValue. It is passed by value (not by reference) so changing it will not change the actual parameter.

    v is returned and is clearly used to update the UI.

    You may want to investigate changing the properties data type to a reference type. Then it will be passed by reference and any changes made will reflect back to source. Assuming the databinding process does not create a copy.

    0 讨论(0)
  • 2020-12-09 19:00

    Wow, that is surprising. When you set a value on a dependency property, binding expressions are updated before value coercion runs!

    If you look at DependencyObject.SetValueCommon in Reflector, you can see the call to Expression.SetValue halfway through the method. The call to UpdateEffectiveValue that will invoke your CoerceValueCallback is at the very end, after the binding has already been updated.

    You can see this on framework classes as well. From a new WPF application, add the following XAML:

    <StackPanel>
        <Slider Name="Slider" Minimum="10" Maximum="20" Value="{Binding Value, 
            RelativeSource={RelativeSource AncestorType=Window}}"/>
        <Button Click="SetInvalid_Click">Set Invalid</Button>
    </StackPanel>
    

    and the following code:

    private void SetInvalid_Click(object sender, RoutedEventArgs e)
    {
        var before = this.Value;
        var sliderBefore = Slider.Value;
        Slider.Value = -1;
        var after = this.Value;
        var sliderAfter = Slider.Value;
        MessageBox.Show(string.Format("Value changed from {0} to {1}; " + 
            "Slider changed from {2} to {3}", 
            before, after, sliderBefore, sliderAfter));
    }
    
    public int Value { get; set; }
    

    If you drag the Slider and then click the button, you'll get a message like "Value changed from 11 to -1; Slider changed from 11 to 10".

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