Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Silverlight > NumericUpDown > Update ViewModel via binding

Not answered Update ViewModel via binding

Feed from this thread
  • THOMAS avatar

    Posted on Nov 28, 2011 (permalink)

    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

                    

    Reply

  • Konstantina Konstantina admin's avatar

    Posted on Nov 29, 2011 (permalink)

    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 >>

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Silverlight > NumericUpDown > Update ViewModel via binding
Related resources for "Update ViewModel via binding"

Silverlight NumericUpDown Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]