WPF:标签未显示在后面的代码中

发布于 2024-11-30 07:41:02 字数 3428 浏览 6 评论 0原文

好吧,所以我一直在寻找答案,但我开始怀疑如果我看到它我是否会知道它。我是 WPF 新手,但使用 C# 开发 Windows 窗体已经有几年了。

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

XML 代码:

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

当我进入代码隐藏文件时,我无法让“LBL_StartTimeConvert”显示在智能感知中。如何从 C# 代码访问此标签?我需要更改标签文本。谢谢!

PS>>如果您能给我提供有关学习 WPF 的任何好的资源/书籍(例如 WPF 101 或 WPF for Dummies),我将不胜感激!

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.

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"

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘东忘西忘不掉你 2024-12-07 07:41:02

我也有同样的问题。重启VS解决了。
有时这确实是一个错误;)

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

简单爱 2024-12-07 07:41:02

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

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

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.

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

喜爱皱眉﹌ 2024-12-07 07:41:02

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文