如何访问WPF Xaml中的控件引用?
我有一些控件,我将它们的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要访问后面代码中的任何元素,您需要设置 x:Name 指令。
它告诉 XAML 解析器将表示命名元素的字段添加到 Window 类的自动生成部分,就像 Winforms 一样。
在 WPF 应用程序中,不需要为每个元素命名。您应该只命名那些您想要以编程方式与之交互的元素。
示例:
编辑:
我使用了以下代码,并且能够访问列表视图:
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:
EDIT:
I used the following code and I was able to access the list view:
您是否在 xaml 中设置了它们的
x:Name="ControlName"
属性?此处是有关 x:Name 指令的更多信息。
例如:
have you set their
x:Name="ControlName"
property in xaml?Here is more information on x:Name directive.
For example: