如何访问WPF Xaml中的控件引用?

发布于 2024-10-23 17:47:27 字数 1284 浏览 1 评论 0原文

我有一些控件,我将它们的 Name 属性设置为唯一名称,但我无法在匹配的 C# 代码文件中访问它们。

我已经尝试过了:

this.ControlName
MainWindow.ControlName
ControlName

但它确实“看到”了它们。

我该怎么做?

另外,我是否必须为包裹面板、网格视图等内的嵌套控件做一些特殊的事情?

编辑:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
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;
using System.Reflection;

namespace EditorWindow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow ( )
        {

        }
    }
}

<Window x:Class="EditorWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Effects Editor">

    <DockPanel>
        <ListView x:Name="EffectsListView">
        </ListView>
    </DockPanel>
</Window>

I have some controls where I set their Name property to unique names, but I am unable to access them in the matching C# code file.

I have tried:

this.ControlName
MainWindow.ControlName
ControlName

but it does "see" them.

How do I do this?

Also do I have to do something special for nested controls inside wrap panels, grid views, etc?

EDIT:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
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;
using System.Reflection;

namespace EditorWindow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow ( )
        {

        }
    }
}

<Window x:Class="EditorWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Effects Editor">

    <DockPanel>
        <ListView x:Name="EffectsListView">
        </ListView>
    </DockPanel>
</Window>

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

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

发布评论

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

评论(2

<逆流佳人身旁 2024-10-30 17:47:27

要访问后面代码中的任何元素,您需要设置 x:Name 指令。
它告诉 XAML 解析器将表示命名元素的字段添加到 Window 类的自动生成部分,就像 Winforms 一样。

在 WPF 应用程序中,不需要为每个元素命名。您应该只命名那些您想要以编程方式与之交互的元素。

示例:

<TextBlock x:Name="tblText" Text="Stackoverflow rocks."></TextBlock>

编辑:
我使用了以下代码,并且能够访问列表视图:

namespace WpfApplicationUnleashed
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {    
        public Window1()
        {
            InitializeComponent();
            EffectsListView.Width = 10;
        }    
    }
}

<Window x:Class="WpfApplicationUnleashed.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApplicationUnleashed"
        Title="Window1" >
    <DockPanel>
        <ListView x:Name="EffectsListView"></ListView>
    </DockPanel>
</Window>

For accessing any element in code behind you will need to set x:Name directive.
It tells the XAML parser to add a field representing the named element to the automatically generated portion of the Window class just like Winforms.

In a WPF application, there’s no requirement to name each and every element. You should name only those elements which you want to programatically interact with.

An example:

<TextBlock x:Name="tblText" Text="Stackoverflow rocks."></TextBlock>

EDIT:
I used the following code and I was able to access the list view:

namespace WpfApplicationUnleashed
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {    
        public Window1()
        {
            InitializeComponent();
            EffectsListView.Width = 10;
        }    
    }
}

<Window x:Class="WpfApplicationUnleashed.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApplicationUnleashed"
        Title="Window1" >
    <DockPanel>
        <ListView x:Name="EffectsListView"></ListView>
    </DockPanel>
</Window>
云仙小弟 2024-10-30 17:47:27

您是否在 xaml 中设置了它们的 x:Name="ControlName" 属性?

此处是有关 x:Name 指令的更多信息。

例如:

<Button x:Name="Button1">Click Me</Button>

have you set their x:Name="ControlName" property in xaml?

Here is more information on x:Name directive.

For example:

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