如何命名WPF控件以便于编码?

时间:2013-12-31 17:30:09

标签: c# wpf wpf-controls

添加多个comboxbox控件时,在双击项目以修改代码后,VS 2013使用通用命名方案,这使得难以保持代码的直接性。例如,ComboBox_SelectionChanged和ComboBox_SelectionChanged_1。

如何重命名WPF控件以使CS更易于跟踪?

(希望这是有道理的,我正在使用正确的术语。如果需要澄清,请告诉我。)

CS代码:

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代码:

<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>

2 个答案:

答案 0 :(得分:1)

场景1:事件处理程序不存在

您可以通过为控件提供具有x:name扩展名属性的名称来影响事件处理程序后面代码的命名。如果您已设置名称并双击控件,您将获得一个名为NameOfControl_SelectionChanged的命名事件处理程序。

场景2:事件处理程序确实存在

如果您已经在代码隐藏文件中创建了事件处理程序,则必须手动重命名后面的代码中的事件处理程序和关联的xaml属性(SelectionChanged="...")。

答案 1 :(得分:1)

实际上,您的ComboBox控件名为“ComboBox_SelectionChanged_1”和“ComboBox_SelectionChanged”。第一个组合框名称(x:Name)是“race_dropdown”,第二个名称没有名称(UI控件在WPF中不需要显式名称)。

“ComboBox_SelectionChanged(_1)”只是处理组合框'SelectionChanged事件的方法的默认名称。如果您在之前给组合框命名(例如x:Name="asdf",则双击它,您将获得一个更好的事件处理程序名称(例如“asdf_SelectionChanged”)。

现在,回答您的问题:您可以随后通过在代码隐藏(CS代码)中重命名方法来更改名称,并在xaml中的SelectionChanged属性中键入相同的名称(WPF代码)