Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access XAML controls from C# code-behind

Tags:

c#

xaml

I have two files in my VS project: Custom.xaml and Custom.cs

In my XAML file, I have the following text boxes:

<TextBox x:Name="TextBox1" Text="{Binding Value, Mode=TwoWay}" Foreground="Black" Background="Green" SelectionChanged="TextBox1_SelectionChanged" />

<TextBox x:Name="TextBox2" Text="{Binding Value, Mode=TwoWay}" Foreground="Black" Background="Green" SelectionChanged="TextBox2_SelectionChanged" />

In my .cs, I have the following method:

 void TextBox1_SelectionChanged(object sender, RoutedEventArgs e)
 {
     TextBox t = e.Source as TextBox
 }

I can successfully hit the event handler above. Then, I can grab TextBox1 and it's properties by using e.Source, but I would like to access TextBox2 and it's properties.

As a sidenote, the .cs file is just a C# class that I am referencing, not a xaml.cs. Additionally, I understand that I could implement this via a UserControl, but cannot do that in this scenario for reasons that are outside the scope of this post.

Please advise on how I can get/set properties of TextBox2.

Thanks.

EDIT: Any other input on this? As a workaround, I've added an event handler called TextBox2_Loaded, and then set e.Source to an instance variable. Then, in TextBox1_SelectionChanged, I can access the instance variable. Would really like to just target the control directly (ex. TextBox2.IsEnabled). I must be missing a declaration or inheritance somewhere. Can't even find the control using FindName.

like image 856
Josh Avatar asked Mar 03 '12 19:03

Josh


People also ask

How can I access a control in WPF from another class or window?

If you want to access a control on a wpf form from another assembly you have to use the modifier attribute x:FieldModifier="public" or use the method proposed by Jean. Save this answer.

How do we refer to WPF controls in code?

Controls in WPF are accessed by their Name properties. We specify the Name property in the XAML, and then can access the control by that name directly in C# code.

What are controls in XAML?

The XAML User Interface framework offers an extensive library of controls that supports UI development for Windows. Some of them have a visual representation such Button, Textbox, TextBlock, etc.; while other controls are used as containers for other controls or content, for example, images.

What is XAML CS in C#?

xaml. cs is the code-behind page for MainPage. xaml. It's where you add your app logic and event handlers. Together these two files define a new class called MainPage , which inherits from Page, in the HelloWorld namespace.


2 Answers

Alright, so I apparently had left out a critical component in this post... My TextBox controls are inside of DataTemplate controls. From my research, the TextBox controls cannot be accessed when inside of DataTemplate controls. I really didn't think that would matter, but I guess the instance variables are not created when this scenario exists.

If I've interpreted this incorrectly, please provide input. For now, I've gone ahead and added a Loaded event and defined my TextBox controls as instance variables so that I can access them and change properties when other activities occur.

Thanks for everyone's input.

like image 179
Josh Avatar answered Oct 20 '22 08:10

Josh


As long as you have set a namein the XAML, you can access it directly by name (The XAML compiler will create an instance variable for you.)

void TextBox1_SelectionChanged(object sender, RoutedEventArgs e) 
{ 
    TextBox t = e.Source as TextBox 
    TextBox2.Text = "Whatever";
} 
like image 25
GazTheDestroyer Avatar answered Oct 20 '22 08:10

GazTheDestroyer