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

时间:2022-05-15 15:45:23

Ok, so I've been looking all over for a anwser, but I starting to wonder if I would even know it if I saw it. I'm new to WPF, but have been doing Windows Forms in C# for a few years now.

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

My Question is: Why can't I access a label from the code behind that I "dragged-n-dropped" on to the grid and renamed?

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

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>

The particular label I'm refereing to is the last label in the first GroupBox named "LBL_StartTimeConvert"

我要引用的特定标签是第一个名为“LBL_StartTimeConvert”的GroupBox中的最后一个标签

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

When I go into the Code behind file, I am unable to get "LBL_StartTimeConvert" to show up in intellisense. How do I access this label from the C# code? I need to change the labels text. Thanks!

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

PS>> Any good resources/books that you can point me to on learning WPF (like WPF 101 or WPF for Dummies) would be much appreciated!

PS >>你可以指导我学习WPF的任何好资源/书籍(如WPF 101或WPF for Dummies)将不胜感激!

3 个解决方案

#1


5  

Please make sure your namespace and window name is same in code behind as xaml file cause when I have pasted your code in my sample window, it working fine and also I can access label LBL_StartTimeConvert.

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

Please create new sample WPF project and put your code which is inside <grid> tag and then check it.

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

#2


18  

I had the same problem. A restart of VS solved it. Sometimes it really is a bug ;)

我有同样的问题。重新启动VS解决了它。有时它真的是一个bug;)

#3


3  

You should use x:Name instead of Name
"x:Name: Specifies a run-time object name for the instance that exists in run-time code after an object element is processed. In general, you will frequently use a WPF-defined equivalent property for x:Name. Such properties map specifically to a CLR backing property and are thus more convenient for application programming, where you frequently use run time code to find the named elements from initialized XAML."
See XAML overview Here

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

#1


5  

Please make sure your namespace and window name is same in code behind as xaml file cause when I have pasted your code in my sample window, it working fine and also I can access label LBL_StartTimeConvert.

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

Please create new sample WPF project and put your code which is inside <grid> tag and then check it.

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

#2


18  

I had the same problem. A restart of VS solved it. Sometimes it really is a bug ;)

我有同样的问题。重新启动VS解决了它。有时它真的是一个bug;)

#3


3  

You should use x:Name instead of Name
"x:Name: Specifies a run-time object name for the instance that exists in run-time code after an object element is processed. In general, you will frequently use a WPF-defined equivalent property for x:Name. Such properties map specifically to a CLR backing property and are thus more convenient for application programming, where you frequently use run time code to find the named elements from initialized XAML."
See XAML overview Here

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