views:

532

answers:

2

Hi.

I'm creating a WPF UserControl with many elements inside. I need to work with them and I need to give them names. I've seen that in some custom controls people are naming elements this way:

PART_name

Should I name my elements like PART_ListBox.. etc? Or what's the purpose of this? Is it to differentiate that these names are part of a UserControl?

Btw my class inhertis directly from UserControl, not from Control.

class MyControl: UserControl {}
+1  A: 

This is the Microsoft standard for internal parts of a Template/Style. These parts are local to the control, so it would depend what the scope of your element that you are naming is.

As for whether or not to use this standard, my opinion is that because all the generic WPF/Silverlight control templates/styles use this convention I would follow the pack on this one, also from my experience, I like the PART_ naming convention, as I find that local elements stand out well within my templates and styles.

Hope that helps.

Chris Nicol
Yes that's what I've been thinking.. although, Jobi's argument is good too.
PaN1C_Showt1Me
Jobi is absolutely correct, this is for CustomControls only. Sorry I didn't notice that you said UserControl.For usercontrols you should follow the standard naming conventions and you should only name an element that is actual being referenced somewhere less.
Chris Nicol
+2  A: 

PART_ convention is needed only in Custom control templates. As you know that a custom control generally be 'lookless' in nature so the developer developing the control might need to assume some XAML controls present in all the customization of that ControlTemplate. So PART_ is a way to let the person doing the XAML Edit(Giving look to the custom control) know that they need to retain the PART_ named controls in the new control template because the code really depends on those.

In your case it is UserControl(Which means the it is not really a look less control) so you don't need to go with PART_ convention here.

Jobi Joy
But does it not increase the chance of giving same names to a control outside and inside the UserControl ?
PaN1C_Showt1Me
That's fine for things outside and inside UserControl to have the same name, there's no clash in this case.
Oleg Mihailik
"There's no clash in this case" <- That's what I wanted to know too. Thanks
PaN1C_Showt1Me