0

I'm using the IntegerUpDown control from WPFExtendedToolkit

However, I'm unable to assign the event my function so that when the value is changed it will call my function. I'm pretty new to both c# and wpf so help is greatly appreciated. I've been trying to get it to work as show in a similar example here.

private IntegerUpDown m_argumentUpDown;

public IntArgumentOption(ArgumentOptionDescription argumentDesc) : base(argumentDesc)
{
    m_argumentUpDown = new IntegerUpDown
    {
        Watermark = argumentDesc.m_watermark,
        Increment = (int)argumentDesc.m_interval,
        Minimum = (int)argumentDesc.m_minimum,
        Maximum = (int)argumentDesc.m_maximum,
        FormatString = "G",
        SelectAllOnGotFocus = true,
        MinWidth = 50,
        FontSize = 10,
        Margin = new System.Windows.Thickness(5, 0, 0, 0),
    };

    // COMPILER ERROR:
    m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<int>(ArgumentChanged);
}

void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<int> e)
{

}

Doing this results in the compiler error:

error CS0029: Cannot implicitly convert type 'System.Windows.RoutedPropertyChangedEventHandler< int >' to 'System.Windows.RoutedPropertyChangedEventHandler< object >'

Community
  • 1
  • 1

2 Answers2

0

The following will work, I tested that. But I dont know if this is considered work around or creator of IntegerUpDown control meant it to be used this way.

m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<object>(ArgumentChanged);
//or you can change above line to following for brevity. ReSharper always suggesting me to do this
//m_argumentUpDown.ValueChanged += ArgumentChanged;

void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    //you need to cast New and Old value to int since both are of type object now
    int newVal = (int)e.NewValue;
    int oldVal = (int)e.OldValue;
}
har07
  • 88,338
  • 12
  • 84
  • 137
0

Here in the UpDownBase class (Xceed.wpfToolkit.dll) the method signature for ValueChanged is:

public event RoutedPropertyChangedEventHandler<object> ValueChanged;

hence in your code you have to declare a event handler where the generic is of type "Object" instead of int. Because of the mismatch in type the compiler is unable to implicitly convert to Int from object. So change code as below

m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<object>(ArgumentChanged);

    }

    void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
           //type caste e.newValue and e.OldValue 
    }