3

Here is one that bothers me!

I found on stackoverflow this Accessing XAML object in codebehind(WPF) about accessing a resource.

But what if i want reference to a textbox, for example, in my code-behind file? I didn't find that around.

Thanks!

Community
  • 1
  • 1
Andr
  • 617
  • 2
  • 9
  • 24

2 Answers2

4

Give it an x:Name and then in your code-behind just reference it as this.TheName.

See XAML Named Elements on MSDN.

sourcenouveau
  • 29,356
  • 35
  • 146
  • 243
2

XAML:

<TextBox Name="txtBox"  Text="Example Text"/>

Code:

  txtBox.Foreground = Brushes.Blue;

But not all objects in xaml has property called Name, in that case you can use this:

<SomeObject x:Name="namedObject" .../>
Nawaz
  • 353,942
  • 115
  • 666
  • 851