Setting a custom FieldStyle causes OnFieldLoaded to be called twice for each property

2 Answers 95 Views
PropertyGrid
Andrzej
Top achievements
Rank 1
Iron
Andrzej asked on 21 Sep 2022, 08:34 AM | edited on 21 Sep 2022, 08:35 AM

Hello,

I am experiencing the same issue, that "Guy" has mentioned in his thread:

Link to question

However, the topic is discontinued. So the issue is, that when i use classic field style, everything looks good. My object has 4 properties, and the event FieldLoaded is called 4 times - see counter at the bottom.

However, when i apply FieldStyle (in my case FieldStyle="{StaticResource PropertyGridFieldStyleCustom}") to make property name appear over editor instead of on the left on it, the fieldloaded event is called 8 times - twice for each property.

I am attaching my solution where You can see the example im referring to. Is there something im doing wrong, or it may be a RadPropertyGrid bug?

Thanks for any help.

2 Answers, 1 is accepted

Sort by
1
Accepted
Dilyan Traykov
Telerik team
answered on 22 Sep 2022, 11:50 AM

Hello Andrzej,

Thank you very much for the provided images and sample project.

I tested this at my end and came to the conclusion that the FieldLoaded event is fired twice only if the ControlTemplate property of the PropertyGridField control is overridden.

The reason why it is called twice is that the overridden OnApplyTemplate method of the PropertyGridField class (inherited from the FrameworkElement class) is fired twice in this scenario. The method in turn invokes the FieldLoaded event upon each execution.

With this said, I assume this is some specific of the WPF framework that calls the OnApplyTemplate if the default control template of a control is overridden. You can forward your inquiry to the MSDN and/or StackOverflow forums if you want more details on why this occurs.

Going back to your particular scenario, you can keep track of the already loaded fields and execute your custom logic only once for a given field with the following code:

        private List<PropertyGridField> fields = new List<PropertyGridField>();

        private void RadPropertyGrid1_OnFieldLoaded(object sender, FieldEventArgs e)
        {
            if (!fields.Contains(e.Field))
            {
                ++Counter;
                fields.Add(e.Field);
            }
        }

Can you please give this a try and let me know if such an approach would work for you?

Andrzej
Top achievements
Rank 1
Iron
commented on 23 Sep 2022, 05:33 PM

Thank You for Your explanation! Yes, this is exactly how we worked around this and its working okay so far.  I was just curious why it behaves like that and whether something is wrong on our end. I won't dig too much into it in that case.

Thank You again.
0
Andrzej
Top achievements
Rank 1
Iron
answered on 23 Sep 2022, 05:34 PM
Thank You for Your explanation! Yes, this is exactly how we worked around this and its working okay so far.  I was just curious why it behaves like that and whether something is wrong on our end. I won't dig too much into it in that case.

Thank You again.
Tags
PropertyGrid
Asked by
Andrzej
Top achievements
Rank 1
Iron
Answers by
Dilyan Traykov
Telerik team
Andrzej
Top achievements
Rank 1
Iron
Share this question
or