user481758
user481758

Reputation:

Is there a standard naming convention for WPF UI controls?

it exist some standard for notation of UI controls in WPF. Something like hungarian notation.

For example

<TextBox Name=tbNameOfTextBox/>
<Image   Name=imgOfMe/>
<RichTextBox Name="rtbDocument"/>

What kind of notation do you use?

And my second question is about sequence, organization properties in element.

Fot example I have this:

<ListBox Name="friendsListBox" 
         ItemsSource="{Binding}" 
         SelectedItem="Key"
         Style="{DynamicResource friendsListStyle}"
         PreviewMouseRightButtonUp="ListBox_PreviewMouseRightButtonUp"
         PreviewMouseRightButtonDown="ListBox_PreviewMouseRightButtonDown" 
         Grid.Row="2" 
         Margin="4,4,4,4"
         MouseRightButtonDown="FriendsListBoxMouseRightButtonDown">

or

    <TextBox Name="TbStatus"
             Text="{Binding Path=Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             Style="{StaticResource CurveTextBox}"
             Grid.Column="1" 
             TextWrapping="Wrap" 
             Margin="3,3,3,3" LostFocus="TbStatus_LostFocus" />

In listbox I have properties such as Name, ItemSource,SelectedItem, and som Events. What is appropriate to their organization. The first should be the name of the UI control, then the events and finally style properties?

Upvotes: 5

Views: 6560

Answers (2)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84657

I think mostly this is a matter of personal preferences so many people may disagree with me here. Just answering how I do this.

For your first question
We had a long ongoing discussion about this at my former company and finally we decided to go with camel casing ending with the control type and no abbreviations. So in your case

<TextBox Name="nameOfTextBox"/> 
<Image Name="meImage"/> 
<RichTextBox Name="documentRichTextBox"/>

This is for sure no standard but I use it all the time

For your second question
I think a good way is to place the Name property first, then attached properties (like Grid.Row, Grid.Column etc) and then the Style. After that group by categories, like layout, visibilty etc. The events should come last. Example

<ListBox Name="friendsListBox"
         Grid.Row="2"
         Style="{DynamicResource friendsListStyle}"
         ItemsSource="{Binding}"
         SelectedItem="Key"
         Margin="4,4,4,4"
         PreviewMouseRightButtonUp="friendsListBox_PreviewMouseRightButtonUp"
         PreviewMouseRightButtonDown="friendsListBox_PreviewMouseRightButtonDown"
         MouseRightButtonDown="friendsListBox_MouseRightButtonDown">

<TextBox Name="statusTextBox"
         Grid.Column="1"
         Style="{StaticResource CurveTextBox}"
         Text="{Binding Path=Message,
                        Mode=TwoWay,
                        UpdateSourceTrigger=PropertyChanged}"
         TextWrapping="Wrap"
         Margin="3,3,3,3"
         LostFocus="statusTextBox_LostFocus" /> 

Upvotes: 2

Andrii
Andrii

Reputation: 2462

1) Control naming

Post suggested by Cody Gray should be sufficient WPF UI element naming conventions

2) Event naming

I prefer to use the same convention as Visual Studio "<ControlName>_<EventName>". When you type event name and hit TAB it generates method for you. Don't see any reason to spend time on renaming it, other than using it on multiple controls - in such case you'd want to provide some generic descriptive name for the handler.

3) Attributes order

Mine is: first name (if exists), events last. All other attributes should be grouped by their impact - appearance related (background, foreground, opacity, etc.), layout related (height, width, aligments), etc. Not to have "width" in top of your attributes list and "height" somewere in the bottom.

Upvotes: 6

Related Questions