views:

118

answers:

3

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"&gt;
    <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.

+1  A: 

Name it however you named the XAML file.

Foo.xaml:

<UserControl x:Name="foo" ...
Bryan Watts
A: 

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>

HTH, Kent

Kent Boogaart
I'm liking "Root" so far...
Justin
Is that intended to be the root of the user control, or the root of the XAML tree? You know what you meant, but the next person doesn't. Does it make sense to name everything "_root", even though there are probably better, more context-specific names?
Bryan Watts
@Bryan: Hopefully the next person can read code. I could make a similar argument re your convention.
Kent Boogaart
A: 

Be descriptive; be consistent.

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

Steven