Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming of WPF-Controls in Markup

Tags:

wpf

As I stated in another post, I access at times controls direct from the code behind. If I do this I name the controls as I do also in ControlTemplates and DataTemplates, I give them the prefix PART_ and after that I precise the name, for example…

<TextBox Name=”PART_UserName” />

Is this against a common WPF naming scheme? Formerly I used the prefix m_ followed by the description…

<TextBox Name=”m_userName” />

.. because in reality its exactly this, but I don’t liked it. What do you think? Please note, I don’t want to discuss whether to use MVVM or not. There are situations I don’t care about.

like image 243
HCL Avatar asked Jul 18 '10 17:07

HCL


1 Answers

The pattern PART_<name> is used when you write custom controls that can be retemplated. These are the names of mandatory parts of the template, without which the control cannot work. Such names are usually declared with a [TemplatePart] attribute in C# code :

[TemplatePart(Name = "PART_TextBox", Type = typeof(TextBox))]
[TemplatePart(Name = "PART_UpButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_DownButton", Type = typeof(Button))]
public class NumericUpDown : Control
{
    ...

     protected override OnApplyTemplate()
     {
         base.OnApplyTemplate();

         // Find controls in the template
         TextBox textBox = Template.FindName("PART_TextBox", this) as TextBox;
         Button upButton = Template.FindName("PART_UpButton", this) as Button;
         Button downButton = Template.FindName("PART_DownButton", this) as Button;

         // Do something with the controls...
         ...
     }

    ...
}

So unless you're writing a control template, there's no reason to use that naming pattern. Just name your controls whatever you like, as long as it makes sense for you.

like image 114
Thomas Levesque Avatar answered Oct 17 '22 08:10

Thomas Levesque