如何访问控制在WPF XAML参考? [英] How to access to control references in WPF Xaml?

查看:152
本文介绍了如何访问控制在WPF XAML参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些控制,我设置其名称属性唯一的名称,但我无法访问它们对应的C#code文件。

我曾尝试:

  this.ControlName
MainWindow.ControlName
控件名称
 

但它确实看见他们。

我如何做到这一点?

还做我必须做一些特别的包装板,网格的意见,等等?

里面嵌套控制

编辑:

 使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq的;
使用System.Text;
使用System.Windows;
使用System.Windows.Controls的;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用的System.Reflection;

命名空间EditorWindow
{
    ///<总结>
    ///为MainWindow.xaml交互逻辑
    ///< /总结>
    公共部分类主窗口:窗口
    {
        公共主窗口()
        {

        }
    }
}

<窗​​口x:类=EditorWindow.MainWindow
        的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
        标题=效果编辑器>

    < D​​ockPanel中>
        < ListView的X:名称=EffectsListView>
        < / ListView控件>
    < / DockPanel中>
< /窗>
 

解决方案

有关访问任何元素在code背后,你需要设置X:名称指令。 它告诉XAML解析器添加一个字段重新$ P $指定元素psenting到Window类一样的WinForms的自动生成的部分。

在一个WPF应用程序,也没有要求每个元素名称。您应该名存实亡要以编程方式进行交互这些元素。

一个例子:

 < TextBlock的X:名称=tblText文本=#1石头>< / TextBlock的>
 

编辑:
我用下面的code和我能够访问

 命名空间WpfApplicationUnleashed
{
    ///<总结>
    ///为Window1.xaml交互逻辑
    ///< /总结>
    公共部分类窗口1:窗口
    {
        公共窗口1()
        {
            的InitializeComponent();
            EffectsListView.Width = 10;
        }
    }
}

<窗​​口x:类=WpfApplicationUnleashed.Window1
        的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
        的xmlns:SYS =CLR的命名空间:系统;装配= mscorlib程序
        XMLNS:地方=CLR的命名空间:WpfApplicationUnleashed
        标题=窗口1>
    < D​​ockPanel中>
        < ListView的X:名称=EffectsListView>< / ListView控件>
    < / DockPanel中>
< /窗>
 

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>

解决方案

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

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>

这篇关于如何访问控制在WPF XAML参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆