Search code examples
silverlightsilverlight-toolkit

numericUpDown ValueChanged after MouseLeftButtonDown


I have 2 controls on a form. One numericUpDown (from the Silverlight Toolkit) and a simple Rectangle.

On the MouseLeftButtonDown of the Rectangle I popup a MessageBox with the numericUpDown value.

If I use the arrows to change the value of the numericUpDown, everyting is fine. But if I edit the value manually (with the keyboard) and immediately click on the Rectangle it shows the previous value of the numericUpDown. If I click a sencond time on the rectangle it will show the new value.

The numericUpDown.ValueChanged event is raised after the Rectangle.MouseLeftButtonDown event.

Is that a Silverlight bug? Anybody knows a workaround for that?

(btw I cannot change my Rectangle controls or events)


Solution

  • As workaround I propose you to create your own control like:

    public class MyNumericUpDown : NumericUpDown
        {
            private TextBox _textBox;
            public void Sync()
            {
                ApplyValue(_textBox.Text);
            }
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                _textBox = (TextBox)GetTemplateChild("Text");
            }
        }
    

    Now you can use method Sync to syncronize display text with control Value property. You can call method from XAML declaratively or in code behind. In your case:

    private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                myNumericUpDown.Sync();
                MessageBox.Show(myNumericUpDown.Value.ToString());
            }