C# XAML Introduction Part 4 Object Names Part 1


Code Behind and Object Names

This discussion is based on the book Illustrated WPF by Daniel S on page 86 (Chapter 4). There’s a difference creating class objects with C# and creating them with XAML. Objects created in C# have names. For example, the button created in the following line of C# code has the name btn:

Button btn = new Button();

Objects created in XAML, by default, don’t have names. For example, the following button created in XAML doesn’t have a name:

<Button>Hi There</Button>

If an object created in XAML doesn’t have a name and it’s not manipulated by the code in the code-behind, then there’s no problem. If, however, you need to manipulate the object from the code behind, then the object needs a name. Many classes have a Name property, which you can set using an attribute, if you need to manipulate the object, as shown in the following line of code. Using this form, you can now refer to the button in the code-behind with the name myButton.

<Button Name="myButton">Click Me</Button>

Some classes, however, don’t have a Name property. In this case, you can use a XAML construct for assigning a name, as shown in the following line of code. Using this format you can also refer to the object as myButton in the code-behind.

<Button x:Name="myButton">Click Me</Button>

A Simple Example

Here is a simple example of getting a text string that is in a text box and displaying it in the message box when the user clicks the button. The button’s name is button and the text box’s name is textBox.

ObjectNames

Click button…

ObjectNamesMsgBox

<Window x:Class="ObjectNames.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ObjectNames"
        mc:Ignorable="d"
        Title="Object Names" Height="150" Width="325">
    <Grid>
        <Button x:Name="button" Content="Click Me" HorizontalAlignment="Left" Margin="107,57,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="21" Margin="65,10,0,0" TextWrapping="Wrap" Text="some text" VerticalAlignment="Top" Width="170"/>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace ObjectNames
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            // get the text in the text box:
            string myText = textBox.Text;
            MessageBox.Show("The message in the text box is:  " + myText);
        }
    }
}

An example of setting a custom colour of a rectangle is demonstrated in Part 2 of this series of posts.

Leave a comment

Your email address will not be published. Required fields are marked *