How to access to control references in WPF Xaml?

蓝咒 提交于 2019-12-04 19:27:23

问题


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>

回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/5316062/how-to-access-to-control-references-in-wpf-xaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!