namg_engr
namg_engr

Reputation: 359

creating ValueChanged event handler in code behind

I'm using the DoubleUpDown control from WPFToolkit and I'm trying to create an event handler using ValueChanged.

DoubleUpDown dud = new DoubleUpDown();
dud.ValueChanged += new RoutedPropertyChangedEventHandler<double>(DoubleUpDown_ValueChanged);

private void DoubleUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{

}

I get the error message

CS0029 Cannot implicitly convert type 'System.Windows.RoutedPropertyChangedEventHandler double' to 'System.Windows.RoutedPropertyChangedEventHandler object'

Any suggestions on how this can be addressed to ensure no type conflicts? Thanks.

Upvotes: 0

Views: 1215

Answers (4)

phiphophe
phiphophe

Reputation: 1

i hope this may help you

 this.dud.ValueChanged += new System.Windows.RoutedPropertyChangedEventHandler<double>(this.dud_ValueChanged);


private void dud_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

Upvotes: 0

Calimero
Calimero

Reputation: 319

Is this what you're intending ?

dud.ValueChanged += DoubleUpDown_ValueChanged;
private void DoubleUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
     if(e != null && Math.Abs((double)e.NewValue) < 0.000000001d)
     {
        // for example
     }

}

Upvotes: 0

Gabriel Pr&#225;
Gabriel Pr&#225;

Reputation: 1407

As the erros suggests, ValueChanged is expecting a RoutedPropertyChangedEventHandler<object>, so you would have to do this:

DoubleUpDown dud = new DoubleUpDown();
dud.ValueChanged += new RoutedPropertyChangedEventHandler<object>(DoubleUpDown_ValueChanged);

private void DoubleUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{

}

And inside the handler you will have to cast the object to a double.

Note:

The author left a comment in the source code about that, here:

Due to a bug in Visual Studio, you cannot create event handlers for generic T args in XAML, so I have to use object instead.

Upvotes: 1

Richardissimo
Richardissimo

Reputation: 5775

I've just had a delve through the online source code, and it looks like the declaration of that event is...

public event RoutedPropertyChangedEventHandler<object> ValueChanged

So your signature needs to match that by making it...

private void DoubleUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)

Upvotes: 1

Related Questions