Discussion FrameworkElementFactory AddHandler

  • Friday, August 15, 2008 1:16 PM
     
     
    I can't get this to work. It keeps throwing an exception that says "Handler is an invalid type."

    Code:
     

    factory.AddHandler(TextBlock.DragLeaveEvent, new RoutedEventHandler(Test));

    private void Test(object sender, RoutedEventArgs e)

    {

    SetTitleTemplate();

    }

    What am I doing wrong?  DragLeaveEvent is of Type RoutedEvent.

    Thanks.

    • Changed Type Marco Zhou Wednesday, August 20, 2008 8:20 AM problem solved by OP herself or himselff
    • Changed Type Marco Zhou Wednesday, August 20, 2008 8:20 AM problem solved by OP herself or himselff
    • Changed Type Marco Zhou Wednesday, August 20, 2008 8:20 AM problem solved by OP herself or himselff
    •  

All Replies

  • Friday, August 15, 2008 3:40 PM
     
     
    Thought I would add the whole code block:

    foreach (Column column in this.Columns)

    {

    DataTemplate template = new DataTemplate();

    FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBlock));

    factory.RemoveHandler(TextBlock.LostMouseCaptureEvent, new RoutedEventHandler(Test));

    factory.SetValue(TextBlock.MinHeightProperty, 10.0);

    factory.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Bottom);

    factory.SetValue(TextBlock.ForegroundProperty, _labelBrush);

    factory.SetValue(TextBlock.TextWrappingProperty, TextWrapping.WrapWithOverflow);

    factory.SetValue(TextBlock.FontSizeProperty, 11.0);

    factory.SetValue(TextBlock.FontFamilyProperty, fontFamily);

    factory.SetValue(TextBlock.TextProperty, column.Title.ToString());

    factory.SetValue(TextBlock.WidthProperty, column.Width);

    factory.AddHandler(TextBlock.LostMouseCaptureEvent, new RoutedEventHandler(Test));

    template.VisualTree = factory;

    column.TitleTemplate = template;

    }

  • Friday, August 15, 2008 4:08 PM
     
     
    NM. I figured it out.
  • Monday, April 27, 2009 9:39 AM
     
     
    Hi Greg,

    I have the exact problem you had.  I cannot find any resolution or even anyone else with the same problem.  Can you please tell me how you resolved the problem?

    Many thanks
  • Wednesday, April 29, 2009 12:26 PM
     
     
    Or may be someone else can help?
  • Wednesday, December 15, 2010 6:32 AM
     
     
    I have the same problem, how did you solve it?
  • Wednesday, December 15, 2010 6:42 AM
     
     

    Ok I got it,

     

    The event which we attach has to be of type System.Windows.RoutedEventHandler and not user defined!

    public event System.Windows.RoutedEventHandler textLostFocus;

     

                // Create TextBox
                FrameworkElementFactory textBoxFactory = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBox));
                textBoxFactory.AddHandler(System.Windows.Controls.TextBox.LostFocusEvent, this.textLostFocus );

     

    works fine :)

  • Thursday, November 03, 2011 10:04 AM
     
     

    Hi  vijaykamat

    I got the same problem but I can't solve it from the way that you done it

    I have a method like this:

      private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)

            {

    // just a break point for now

            }

     

    and my code looks like the below:

      FrameworkElementFactory newTextBlock = new FrameworkElementFactory(typeof(TextBlock));

      newTextBlock.AddHandler(TextBlock.MouseDownEvent, this.TextBlock_MouseDown);

     

      but the last line gave me the below error:

     

     

      Error 1 The best overloaded method match for 'System.Windows.FrameworkElementFactory.AddHandler(System.Windows.RoutedEvent, System.Delegate)' has some invalid arguments

      Error 2 Argument 2: cannot convert from 'method group' to 'System.Delegate'

     

  • Friday, November 04, 2011 10:31 AM
     
     

    TextBlock_MouseDown is the system delegate or u have written it?



    Please Mark as Answered If this answers your question Or UnMark as Answered if it did not.
    Happy to Help :)
    My Site
  • Friday, November 04, 2011 12:00 PM
     
      Has Code
    Thanks for reply. It's system routedevent in WPF and I finally sort it out like the below: newTextBlock.AddHandler(TextBlock.MouseDownEvent, new MouseButtonEventHandler(TextBlock_MouseDown));
  • Friday, May 11, 2012 11:45 AM
     
     
    谢谢!帮了我大忙。
  • Friday, May 11, 2012 11:45 AM
     
     
    谢谢!帮了我大忙。