Reputation: 1901
I have a xaml file with buttons like this
<Button Grid.Column="4" BorderThickness="0">3</Button>
<Button Grid.Column="5" BorderThickness="0">4</Button>
The Problem is I can't manage to control the buttons in C#. I've read about templates and resources and tried it but I am unsure if I did was correct.
Most examples online just have a click function but I wan't full control of the button having all of them in a 2D Array and changing them when I want to.
So How do I point to a button created in the xaml file so that I could do
button.Content = "test";
or whatever.
Upvotes: 0
Views: 1382
Reputation: 464
All the answers about naming buttons in XAML are right.
<Button x:Name="btn0"/>
<Button x:Name="btn1"/>
Naming components make them public, thus you can easily access to them in the code behind. If you want, you can later create List of buttons and add them one by one. And then just access to the list.
Upvotes: 2
Reputation: 9733
You just give the button a name and that name you use in code
<Button x:Name="btnBla"/>
btnBla.Background = Brushes.SaddleBrown;
Upvotes: 1
Reputation: 4116
set in xaml
<Button content="test"/>
or in code behind
<Button x:Name = "MyButton"/>
MyButton.Content = "Test";
Upvotes: 1
Reputation: 603
You should give a Name
to your controls in the XAML like this
<Button Name="FirstButton" Grid.Column="4" BorderThickness="0">3</Button>
Then you'll be able to access it in the code-behind :
FirstButton.Content = "test"
Upvotes: 2