Juice
Juice

Reputation: 2870

How to name a WPF control for easier coding?

When adding multiple comboxbox controls, after doubl clicking the item to modify the code, VS 2013 uses a generic naming scheme which makes it hard to keep the code straight. For example, ComboBox_SelectionChanged and ComboBox_SelectionChanged_1.

How do I rename a WPF control to make the CS easier to keep track of?

(Hopefully this makes sense and that I'm using the correct terminology. Let me know if this needs clarification.)

CS Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace hero_workshop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {

        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}

WPF Code:

<Window x:Class="hero_workshop.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Hero Workshop" Height="768" Width="1366">
    <Grid>
        <Menu IsMainMenu="True" Margin="0,0,0,704" Background="White">
            <MenuItem Header="_File" />
            <MenuItem Header="_Edit" />
            <MenuItem Header="_View" />
            <MenuItem Header="_Window" />
            <MenuItem Header="_Help" />
        </Menu>
        <TabControl HorizontalAlignment="Left" Height="714" Margin="0,23,0,0" VerticalAlignment="Top" Width="868">
            <TabItem Header="Chracter">
                <TabItem.Background>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFF0F0F0" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </TabItem.Background>
                <Grid Background="White">
                    <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="Character Name" VerticalAlignment="Top"/>
                    <TextBox HorizontalAlignment="Left" Height="23" Margin="107,7,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
                    <TextBlock HorizontalAlignment="Left" Margin="10,36,0,0" TextWrapping="Wrap" Text="Race" VerticalAlignment="Top" RenderTransformOrigin="-0.621,-2.567"/>
                    <ComboBox x:Name="race_dropdown" HorizontalAlignment="Left" Margin="107,36,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="ComboBox_SelectionChanged_1"/>
                    <TextBlock HorizontalAlignment="Left" Margin="317,13,0,0" TextWrapping="Wrap" Text="Alignment" VerticalAlignment="Top"/>
                    <ComboBox HorizontalAlignment="Left" Margin="388,13,0,0" VerticalAlignment="Top" Width="155" SelectionChanged="ComboBox_SelectionChanged"/>
                </Grid>
            </TabItem>
            <TabItem Header="Skills &amp; Feats">
                <TabItem.Background>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFF0F0F0" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </TabItem.Background>
                <Grid Background="White"/>
            </TabItem>
            <TabItem Header="Weapons &amp; Armor">
                <TabItem.Background>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFF0F0F0" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </TabItem.Background>
                <Grid Background="White"/>
            </TabItem>
            <TabItem Header="Equipment">
                <TabItem.Background>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFF0F0F0" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </TabItem.Background>
                <Grid Background="White"/>
            </TabItem>
            <TabItem Header="Background">
                <TabItem.Background>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFF0F0F0" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </TabItem.Background>
                <Grid Background="White"/>
            </TabItem>
        </TabControl>
        <ScrollViewer HorizontalAlignment="Left" Height="689" Margin="873,38,0,0" VerticalAlignment="Top" Width="475"/>
    </Grid>

</Window>

Upvotes: 2

Views: 289

Answers (2)

Sphinxxx
Sphinxxx

Reputation: 13017

It's not actually your ComboBox controls that are named "ComboBox_SelectionChanged_1" and "ComboBox_SelectionChanged". The first combobox' name (x:Name) is "race_dropdown" and the second doesn't have a name (UI controls don't need explicit names in WPF).

"ComboBox_SelectionChanged(_1)" are just the default names of the methods that handle the comboboxes' SelectionChanged events. If you give a combobox a name (e.g. x:Name="asdf") before you double-click it, you will get a slighly better event handler name (e.g. "asdf_SelectionChanged").

Now, to answer your question: You can always change the names later on by renaming the method in the code-behind (CS code) and type the same name in the SelectionChanged property in xaml (WPF code).

Upvotes: 1

MUG4N
MUG4N

Reputation: 19717

Scenario 1: Event handler does not exist

You can influence the naming of your code behind event handler by giving your control a name with the x:name extension property. If you have set the name and double click your control you will get a named event handler like NameOfControl_SelectionChanged.

Scenario 2: Event handler does exist

If you already have created the event handler in your code behind file you have to rename both, the event handler in your code behind and the associated xaml property (SelectionChanged="...") manually.

Upvotes: 1

Related Questions