WPF:标签不会显示在代码后面

时间:2011-08-18 06:09:04

标签: wpf wpf-controls code-behind

好的,所以我一直在寻找一个anwser,但我开始怀疑,如果我看到它,我是否会知道它。我是WPF的新手,但几年来一直在C#中使用Windows Forms。

我的问题是:为什么我无法从我“拖放”到网格后面的代码中访问标签并重命名?

XML CODE:

<Window x:Class="IMCommand.NewProblemWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="New Problem" Height="800" Width="800" Loaded="Window_Loaded">
    <Grid>
        <GroupBox Header="Initial Detection and Recording" Height="168" HorizontalAlignment="Left" Margin="12,45,0,0" Name="groupBox1" VerticalAlignment="Top" Width="754" FontSize="15">
            <Grid>
                <Label Content="Application/Server" Height="28" HorizontalAlignment="Left" Margin="6,6,0,0" Name="label1" VerticalAlignment="Top" />
                <TextBox Height="23" HorizontalAlignment="Left" Margin="144,11,0,0" Name="textBox1" VerticalAlignment="Top" Width="324" Background="#FFCBCBCB"></TextBox>

                <Label Content="AE Problem Ticket #" Height="28" HorizontalAlignment="Left" Margin="6,52,0,0" Name="label2" VerticalAlignment="Top" />
                <TextBox Background="#FFCBCBCB" Height="23" HorizontalAlignment="Left" Margin="155,54,0,0" Name="BOX_AEProblem" VerticalAlignment="Top" Width="175" />

                <Label Content="Start Time" Height="28" HorizontalAlignment="Left" Margin="6,86,0,0" Name="label4" VerticalAlignment="Top" Width="85" />
                <TextBox Height="23" HorizontalAlignment="Left" Margin="97,91,0,0" Name="BOX_StartTime" VerticalAlignment="Top" Width="118" IsReadOnly="True" />

                <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="221,89,0,0" Name="LBL_StartTimeConvert" VerticalAlignment="Top" Width="128" />
            </Grid>
        </GroupBox>

        <GroupBox Header="Classification and Initial Support" Height="333" HorizontalAlignment="Left" Margin="12,260,0,0" Name="groupBox2" VerticalAlignment="Top" Width="754" FontSize="15">
            <Grid>
                <Label Content="Description" Height="28" HorizontalAlignment="Left" Margin="6,96,0,0" Name="LBL_Description" VerticalAlignment="Top" />
                <RichTextBox Height="137" HorizontalAlignment="Left" Margin="97,96,0,0" Name="richTextBox1" VerticalAlignment="Top" Width="590" Background="#FFCBCBCB" Cursor="IBeam" ToolTip="Describe the problem..." VerticalScrollBarVisibility="Visible" SpellCheck.IsEnabled="True" FontSize="12" BorderBrush="#FF151414" />
                <RichTextBox Height="28" HorizontalAlignment="Left" Margin="97,44,0,0" Name="richTextBox2" VerticalAlignment="Top" Width="590" Background="#FFCBCBCB" />
                <Label Content="Summary" Height="28" HorizontalAlignment="Left" Margin="6,44,0,0" Name="label3" VerticalAlignment="Top" />
            </Grid>
        </GroupBox>
    </Grid>
</Window>

我所指的特定标签是第一个名为“LBL_StartTimeConvert”的GroupBox中的最后一个标签

<Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="221,89,0,0" Name="LBL_StartTimeConvert" VerticalAlignment="Top" Width="128" />

当我进入Code后面的文件时,我无法让“LBL_StartTimeConvert”显示在intellisense中。如何从C#代码访问此标签?我需要更改标签文字。谢谢!

PS&GT;&GT;您可以指导我学习WPF的任何好资源/书籍(如WPF 101或WPF for Dummies)将非常感谢!

3 个答案:

答案 0 :(得分:19)

我遇到了同样的问题。重新启动VS解决了它。 有时它确实是一个错误;)

答案 1 :(得分:5)

请确保您的名称空间和窗口名称在xaml文件后面的代码相同,因为当我在示例窗口中粘贴代码时,它工作正常,我也可以访问标签LBL_StartTimeConvert。

请创建新的示例WPF项目并将您的代码放在<grid>标记内,然后进行检查。

答案 2 :(得分:3)

您应该使用x:Name而不是Name “x:Name:指定处理对象元素后存在于运行时代码中的实例的运行时对象名。通常,您将经常为x:Name使用WPF定义的等效属性。这样的属性映射特别是CLR支持属性,因此更方便应用程序编程,您经常使用运行时代码从初始化的XAML中查找命名元素。“
请参阅XAML概述Here