Add handler to factory

1.9k Views Asked by At

I can't get this to work.

I need to setup the columns for a datagrid dynamically. One of the columns is a DataGridTemplateColumns with a DoubleUpDown object (from this library)

I create the FrameworkElementFactory like this:

FrameworkElementFactory factory3 = new FrameworkElementFactory(typeof(DoubleUpDown));
factory3.SetValue(DoubleUpDown.ValueProperty, binding);
factory3.AddHandler(DoubleUpDown.ValueChangedEvent, new RoutedEventHandler(UnitsChanged));

public void UnitsChanged(object sender, RoutedEventArgs e)
    {
        //do stuff with e.OriginalSource
    }

This throws an exception of type 'System.Windows.Markup.XamlParseException' when running the application. The problem is in the AddHandler method, if I comment that line it works fine, but obviously I need to attach an event there to do stuff when the value changes.

Can anyone help me, please? Thanks in advance

1

There are 1 best solutions below

0
mm8 On

The handler type is invalid. You should use a RoutedPropertyChangedEventHandler:

public void UnitsChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
     //do stuff with e.OriginalSource
     Console.WriteLine(0);
}

FrameworkElementFactory factory3 = new FrameworkElementFactory(typeof(DoubleUpDown));
                    factory3.SetValue(DoubleUpDown.ValueProperty, 10.0);
                    factory3.AddHandler(DoubleUpDown.ValueChangedEvent, new RoutedPropertyChangedEventHandler<object>(UnitsChanged));