user94392
user94392

Reputation:

Naming Usercontrols. Convention?

So you've got a usercontrol. You would like to bind to some of its dependency properties, so you need specify an x:Name in order to use it.

You can't do this...

<UserControl x:Class="WpfApplication1.UserControl1" x:Name="UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid />
</UserControl>

...because member names cannot be the same as their enclosing type.

So you need to pick something else... but what's a good convention to go for here? Stick something arbitrary on to the end? "UserControl1UserControl"? Call it "Root"? Use a different case "userControl1"?

What choices have you guys been making?

I know this is really minor, but I try to name elements very carefully and consistency is important to me.

Upvotes: 2

Views: 1614

Answers (3)

Kent Boogaart
Kent Boogaart

Reputation: 178630

These names end up as fields in your class, so I just use standard field naming conventions. And if it's the root-level control, I always call it "_root":

<UserControl x:Name="_root">
    <StackPanel>
        <TextBox x:Name="_nameTextBox"/>
        <TextBox x:Name="_ageTextBox"/>
    </StackPanel>
</UserControl>

Upvotes: 0

Steven
Steven

Reputation: 13769

Be descriptive; be consistent.

In other words, just pick something and stick to it.

Upvotes: 1

Bryan Watts
Bryan Watts

Reputation: 45445

Name it however you named the XAML file.

Foo.xaml:

<UserControl x:Name="foo" ...

Upvotes: 1

Related Questions