This is a migrated thread and some comments may be shown as answers.

Update ViewModel via binding

1 Answer 131 Views
NumericUpDown
This is a migrated thread and some comments may be shown as answers.
THOMAS
Top achievements
Rank 1
THOMAS asked on 28 Nov 2011, 11:10 AM

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

                

1 Answer, 1 is accepted

Sort by
0
Konstantina
Telerik team
answered on 29 Nov 2011, 12:55 PM
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 >>
Tags
NumericUpDown
Asked by
THOMAS
Top achievements
Rank 1
Answers by
Konstantina
Telerik team
Share this question
or