添加处理程序到工厂

时间:2016-09-16 09:11:10

标签: c# wpf binding event-handling frameworkelementfactory

我无法让它发挥作用。

我需要动态设置数据网格的列。其中一列是带有DoubleUpDown对象的DataGridTemplateColumns(来自this库)

我像这样创建FrameworkElementFactory

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
    }

这会在运行应用程序时抛出“System.Windows.Markup.XamlParseException”类型的异常。问题出在AddHandler方法中,如果我对该行进行评论它工作正常,但显然我需要在那里附加一个事件来处理值的变化。

有人可以帮帮我吗?提前致谢

1 个答案:

答案 0 :(得分:0)

处理程序类型无效。您应该使用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));