12

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>
Joan Venge
  • 315,713
  • 212
  • 479
  • 689

2 Answers2

22

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>
James A
  • 73
  • 11
Akshay J
  • 5,362
  • 13
  • 68
  • 105
  • So this name has to be unique across all controls? I can access any control with its name no matter how nested it is? – Joan Venge Mar 15 '11 at 19:35
  • 1
    Still doesn't work for some reason. I didn't change the definition of x btw. – Joan Venge Mar 15 '11 at 20:07
  • 2
    No, It doesn't work for me. I have tried x:Name, but it doesn't work. – Christian Irwan Hadi Wicaksana Jul 09 '13 at 05:15
  • 2
    MIght be good to note that by default, simply setting x:Name won't make that control accessible EVERYWHERE in your project, just within the code-behind specifically for the xml where the x:Name is defined. – Lucas Jun 17 '16 at 00:33
  • To make it visible in whole namespace, use a cast to your special Window1: `var mw = (Window1)Application.Current.MainWindow;`, then you can call: `mw.EffectsListView.Width = 10;` – VisorZ Jun 04 '18 at 13:01
5

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>
Lav
  • 1,850
  • 15
  • 17
  • 1
    I did, but didn't use x:Name. I just used Name="ControlName". Why do you use x? Also if it's necessary why did the xaml compiler didn't warn me? What would happen if you omit x? – Joan Venge Mar 15 '11 at 19:34
  • Still doesn't work for some reason. I didn't change the definition of x btw. – Joan Venge Mar 15 '11 at 19:50
  • @JoanVenge Refer to this http://stackoverflow.com/questions/589874/in-wpf-what-are-the-differences-between-the-xname-and-name-attributes question and you will get answer of the diff beween Name and x:Name – Lav Mar 15 '11 at 20:22