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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With