DataEntry property names and display names

1 Answer 100 Views
DataEntry
Patgat
Top achievements
Rank 2
Iron
Iron
Iron
Patgat asked on 18 Sep 2022, 09:59 AM

Hello,

I am using the DataEntry component along with the DisplayName attribut.

However, at run time, I don't a way to fetch the original name (from the model class) as the Text field contains substitute the DisplayName to the original one.

Apparently the control Name field is always empty. Couldn't you use it to store the control name ?

Regards

Patrick

 

 


1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 21 Sep 2022, 12:15 PM

Hello Patrick,

I am not sure that I have fully understood your approach. Upon checking the name of the Editor or the Label, you are right that the Name property is empty. You could easily set it by using the EditorInitialized and ItemInitialized event handler. In the first event, you can get the editor and in the second you can get the label. You can check the Events section of the RadDataEntry in our documentation.

private void RadDataEntry1_EditorInitialized(object sender, EditorInitializedEventArgs e)
{
    e.Editor.Name = e.Editor.GetType().Name;
}

private void RadDataEntry1_ItemInitialized(object sender, ItemInitializedEventArgs e)
{
    e.Panel.Controls[1].Name = e.Panel.Controls[1].GetType().Name;
}

If this is not what you are looking for, you can share some more details. You can use the attached project as a starting point.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Patgat
Top achievements
Rank 2
Iron
Iron
Iron
commented on 24 Sep 2022, 01:03 PM

Hello Dinko,

My apologies as I was not very clear in my request.

I am using the following kind of model with the DataEntry component:

public class FournisseurModel
{
    //[Browsable(false)]  // Pour InputForm
    public string FournisseurID { get; set; }

    [DisplayName("*Société")]
    public string CompanyName{ get; set; } 

etc ...

For each Editor there is therefore a 'ModelName' (here CompanyName or FournisseurID) and a DisplayName (only 'Société' here).

within the ItemInitialized event, the Name field is empty and the DisplayName (when there is one as Attribut in the model)  is stored in the Text field, while if there is no DisplayName Attribut in the model, the Text field contains the ModelName (here FournisseurID).

Therefore the ModelName is apparently overwritten by the DisplayName and lost.

I cannot set it up in the name field of the Label within the EditorInitialized Event, as this provide the name of the control (RadTextBox).

Currently, I have addressed this issue by setting up at form load an equivalence dictionary between ModelNames and DisplayNames from the Model data(see below) , but I find it a bit excessive.


    public static Dictionary<string, string> SetModelToDisplayNamesDictionary(Type t, string attributeName)
    {
        Dictionary<string, string> DictionaryofPropertiesAttributs = new();

        PropertyInfo[] myProperties = t.GetProperties();
        for (int i = 0; i < myProperties.Length; i++)
        {
            object[] myAttributes = myProperties[i].GetCustomAttributes(true);

            if (myAttributes.Length > 0)
            {

                foreach (Attribute attr in myAttributes)
                {
                    if (attr is DisplayNameAttribute)
                    {
                        string res = ((DisplayNameAttribute)attr).DisplayName;
                        DictionaryofPropertiesAttributs.Add(res, myProperties[i].Name);

                    }
                }
            }
        }
        return DictionaryofPropertiesAttributs;
    }

So my question is : Is there a simple way to get during the control setup to store/get somewhere the ModelName for each Editor in addition to the DisplayName ?

Thanks

Patrick

 

Dinko | Tech Support Engineer
Telerik team
commented on 27 Sep 2022, 10:11 AM

Thank you for the additional details. I see what you mean here. This is the default behavior when the DisplayName attribute is set above the property. The control will use the attribute for the label text. In your case, what I can suggest is to subscribe to the EditorInitialized event of the RadDataEntry. In the event handler, you could get the property name from the e.Property (PropertyDescriptor). Then assign it to the Tag property. Then subscribing to the ItemIniitialized event you have access to the label and its editor control. What you need to do is to set the Text property to the Tag property of the editor. This way you can override the DisplayName and yet again use the property name as a display name.

private void RadDataEntry1_EditorInitialized(object sender, EditorInitializedEventArgs e)
{
    e.Editor.Tag = e.Property.Name;
}

private void RadDataEntry1_ItemInitialized(object sender, ItemInitializedEventArgs e)
{
    e.Panel.Controls[1].Text = e.Panel.Controls[0].Tag.ToString();
}

 I hope this approach will work for you.
Tags
DataEntry
Asked by
Patgat
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or