FrameworkElementFactory.AddHandler not working

7 posts, 0 answers
  1. Michael
    Michael avatar
    8 posts
    Member since:
    Feb 2012

    Posted 18 Feb 2013 Link to this post

    I am using a RadPropertyGrid. I am trying to use the AutoGeneratingPropertyDefinition event to provide a custom EditorTemplate with a textbox. I want to add a handler to the TextChanged event of the textbox but my event handler is never being called.

    Relevant code:
    public void GeneratingProperty(AutoGeneratingPropertyDefinitionEventArgs e)
    {
        try
        {
            AvailableProperties property = (AvailableProperties)Enum.Parse(typeof(AvailableProperties), e.PropertyDefinition.Binding.Path.Path);
            if (IsAvailable(item.GetAvailableProperties(), property))
            {
                switch (property)
                {
                    case AvailableProperties.Title:
                        FrameworkElementFactory textBoxFactory = new FrameworkElementFactory(typeof(TextBox));
                        textBoxFactory.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TitleTextChanged));
                        textBoxFactory.SetValue(TextBox.BackgroundProperty, System.Windows.Media.Brushes.Tomato);
                        textBoxFactory.SetValue(TextBox.BorderBrushProperty, System.Windows.Media.Brushes.Transparent);
                        textBoxFactory.SetBinding(TextBox.TextProperty, new Binding("Title"));
     
                        DataTemplate template = new DataTemplate();
                        template.VisualTree = textBoxFactory;
                        template.Seal();
     
                        e.PropertyDefinition.EditorTemplate = template;
                        e.PropertyDefinition.OrderIndex = 0;
                        e.PropertyDefinition.DisplayName = "Title";
                        e.PropertyDefinition.Description = "The title of the attribute. Can only contain letters, numbers, and underscores.";
     
                        break;
                    default:
                        // other properties go in case statements above here
                        break;
                }
            }
            else
            {
                e.Cancel = true;
            }
        }
        catch
        {
            e.Cancel = true;
        }
    }
     
    public void TitleTextChanged(object sender, TextChangedEventArgs e)
    {
        if (sender is TextBox)
        {
            TextBox titleTextBox = (TextBox)sender;
            int caretIndex = titleTextBox.CaretIndex;
            string newValue = titleTextBox.Text;
     
            item.Title = newValue;
            ApplyCorrectedValue(newValue, item.Title, titleTextBox, caretIndex);
     
            NotifyOfPropertyChange(() => MappingsPanel);
        }
        else
        {
            Logger.Log("TitleChanged: source must be a TextBox");
            throw new ArgumentException("source must be a TextBox");
        }
    }
    I am using the 2012.2.912.35 version of the telerik dlls
  2. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 19 Feb 2013 Link to this post

    Hi,

    Unfortunately, this is a framework limitation of WPF. DataTemplates constructed with FrameworkElementFactory lose their event subscriptions, as soon as DataTemplate.LoadContent() gets invoked. In case you are willing to upgrade to a newer version, we have recently introduced a FieldLoaded event, where you can access the editor and subscribe to some of its events.

    Greetings,
    Ivan Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Michael
    Michael avatar
    8 posts
    Member since:
    Feb 2012

    Posted 19 Feb 2013 Link to this post

    Hi Ivan,

    That doesn't seem right. I have written another small WPF application that just has a content control which uses a button created in a FrameworkElementFactory.

    C# Code:
    void btn_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Clicekd!");
    }
     
    private void cc1_Loaded(object sender, RoutedEventArgs e)
    {
        FrameworkElementFactory fef = new FrameworkElementFactory(typeof(Button));
        fef.AddHandler(Button.ClickEvent, new RoutedEventHandler(btn_Click));
        fef.SetValue(Button.ContentProperty, "Button2");
        DataTemplate dt = new DataTemplate();
        dt.VisualTree = fef;
        dt.Seal();
        dt.LoadContent();
        (sender as ContentControl).ContentTemplate = dt;
    }
    XAML:
    <Grid>
        <ContentControl x:Name="cc1" Grid.Row="0" Loaded="cc1_Loaded"/>
    </Grid>

    This code calls the event handler call-back when the button is pressed.

    Regards,
    Michael

  5. Kristoffer
    Kristoffer avatar
    158 posts
    Member since:
    Dec 2012

    Posted 05 Jun 2013 Link to this post

    Any news regarding this? I'm trying to build a template in code behind (to solve this).

    My event handler isn't working :(

    buttonElement.AddHandler(Button.ClickEvent, new RoutedEventHandler(SelectFile));

  6. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 07 Jun 2013 Link to this post

    Hello,

    Unfortunately this is a framework limitation of WPF. I have prepared a sample project that demonstrates it out of the scope of RadControls. You can see the difference between XAML defined DataTemplate and FrameworkElementFactory.

    Regards,
    Ivan Ivanov
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  7. JC
    JC avatar
    2 posts
    Member since:
    Mar 2013

    Posted 19 Sep 2013 Link to this post

    That is FieldLoaded event ?, I could give an example  please?ç

    Thanks.

    Jose Carlos Vasquez.
  8. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 20 Sep 2013 Link to this post

    Hello Jose,

    I am not sure that I understand your questions properly. Would you please be more specific and explain in details? 

    Regards,
    Didie
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top
UI for WPF is Visual Studio 2017 Ready