14

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> 
4
  • Can you post sample of your xaml and codebehind files? Commented Mar 15, 2011 at 20:23
  • @Lav: Ok posted the code. Thanks again. Commented Mar 15, 2011 at 20:32
  • 1
    please dont delete the InitializeComponent method in the constructor. Commented Mar 16, 2011 at 2:15
  • Sorry I deleted it while posting, in the actual code it exists. Commented Mar 16, 2011 at 3:02

2 Answers 2

24

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> 
Sign up to request clarification or add additional context in comments.

5 Comments

So this name has to be unique across all controls? I can access any control with its name no matter how nested it is?
Still doesn't work for some reason. I didn't change the definition of x btw.
No, It doesn't work for me. I have tried x:Name, but it doesn't work.
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.
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;
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> 

3 Comments

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?
Still doesn't work for some reason. I didn't change the definition of x btw.
@JoanVenge Refer to this stackoverflow.com/questions/589874/… question and you will get answer of the diff beween Name and x:Name

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.