Naming custom controls. Convention?

So you have a usercontrol. You want to bind some of its dependency properties, so you need to specify x: Name to use it.

You cannot 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 have to choose something else ... but what's a good convention to go here? Attach something arbitrary to the end? "UserControl1UserControl"? Call it "Root"? Use another case of "userControl1"?

What options did you guys do?

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

+2


source to share


3 answers


Name it, however you named the XAML file.

Foo.xaml:



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

      

+1


source


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



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

      

0


source


Be descriptive; be consistent.

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

0


source







All Articles