This is a migrated thread and some comments may be shown as answers.

FrameworkElementFactory.AddHandler not working

6 Answers 940 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 19 Feb 2013, 03:59 AM
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

6 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 19 Feb 2013, 09:58 AM
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.

0
Michael
Top achievements
Rank 1
answered on 19 Feb 2013, 08:40 PM
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

0
Kristoffer
Top achievements
Rank 1
answered on 05 Jun 2013, 08:34 AM
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));

0
Ivan Ivanov
Telerik team
answered on 07 Jun 2013, 11:32 AM
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.

0
JC
Top achievements
Rank 1
answered on 19 Sep 2013, 04:03 PM
That is FieldLoaded event ?, I could give an example  please?รง

Thanks.

Jose Carlos Vasquez.
0
Dimitrina
Telerik team
answered on 20 Sep 2013, 08:23 AM
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 >>
Tags
PropertyGrid
Asked by
Michael
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Michael
Top achievements
Rank 1
Kristoffer
Top achievements
Rank 1
JC
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or