Update ViewModel via binding

2 posts, 0 answers
  1. THOMAS
    THOMAS avatar
    12 posts
    Member since:
    Jan 2011

    Posted 28 Nov 2011 Link to this post

    Dear sir,

    I use the RadNumericUpDown control in Silverlight and cannot update the Value property in my ViewModel via binding explicitly. First there is no event that tells me, when the text in the control changed. Second the reaction on KeyUp event and an explicit update of the BindigExpression.UpdateSource doesn’t work.

    XAML:

    <telerik:RadNumericUpDown VerticalAlignment="Center" Value="{Binding Dto.Laufzeit, Mode=TwoWay}" ShowButtons="False"

                                           ValueFormat="Numeric" NumberDecimalDigits="0" Minimum="-9999" Maximum="99999" SmallChange="1" LargeChange="10" Margin="5,0,5,5"

                                           Visibility="{Binding Dto.IsLaufzeitVisible, Converter={StaticResource boolToVisibilityConverter}}" IsEditable="{Binding Dto.IsLaufzeitEditable}"

                                           Views:NumericUpDownBindingHelper.UpdateSourceOnChange="True">

                                <i:Interaction.Behaviors>

                                    <Views:CldbControlStyleBehavior DefaultStyle="{StaticResource CldbNumericUpDownStyle}" ExtendedStyle="{StaticResource CldbNumericUpDownMustStyle}"

                                                              IsExtended="{Binding  Path=Dto.IsLaufzeitRequired}"

                                                              IsExtendedEnabled="{Binding Dto.IsEditable}"/>

                                </i:Interaction.Behaviors>

                            </telerik:RadNumericUpDown>

    The behavior:

    public class NumericUpDownBindingHelper

        {

            public static bool GetUpdateSourceOnChange(DependencyObject obj)

            {

                return (bool)obj.GetValue(UpdateSourceOnChangeProperty);

            }

            public static void SetUpdateSourceOnChange(DependencyObject obj, bool value)

            {

                obj.SetValue(UpdateSourceOnChangeProperty, value);

            }

            public static readonly DependencyProperty UpdateSourceOnChangeProperty =

                DependencyProperty.RegisterAttached("UpdateSourceOnChange", typeof(bool), typeof(NumericUpDownBindingHelper), new PropertyMetadata(false, OnPropertyChanged));

            private static void OnPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)

            {

                var control = obj as RadNumericUpDown;

                if (control == null)

                    return;

                if ((bool)e.NewValue)

                {

                    control.KeyUp += control_KeyUp;

                    control.ValueChanged += OnValueChanged;

                }

                else

                {

    control.KeyUp -= control_KeyUp;

                    control.ValueChanged -= OnValueChanged;

                }

            }

            static void control_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)

            {

                var control = sender as RadNumericUpDown;

                if (control == null)

                {

                    return;

                }

                var be = control.GetBindingExpression(RadNumericUpDown.ValueProperty);

                if (be != null)

                {

                    be.UpdateSource();

                }

            }

            private static void OnValueChanged(object sender, RadRangeBaseValueChangedEventArgs e)

            {

                var control = sender as RadNumericUpDown;

                if (control == null)

                {

                    return;

                }

                var be = control.GetBindingExpression(RadNumericUpDown.ValueProperty);

                if (be != null)

                {

                    be.UpdateSource();

                }

            }

        }

    ViewModel:

    private int? _laufzeit;

            public int? Laufzeit

            {

                get { return _laufzeit; }

                set

                {

                    _laufzeit = value;

                    RaisePropertyChanged(() => Laufzeit);

                    Validate();

                }

            }

    What should I do, to change the Value on every keyup event?

    Best Regards

                    

  2. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 29 Nov 2011 Link to this post

    Hi Thomas,

    In order to update the Numeric on every key stroke you have to set its UpdateValueEvent property to  PropertyChanged.

    Please let us know if that resolves the issue.

    All the best,
    Konstantina
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  3. DevCraft banner
Back to Top