C# XAML Introduction Part 4 Object Names Part 1


This entry is part 6 of 7 in the series C# XAML

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. We can see that the type of the variable is Button, because it is mentioned first. Basic C# declaration of variables has the format of type identifier = value.

Objects created in XAML, by default, don’t have names. You can give them names. Normally you would just use Name=myIdentifier. However, some elements do not have a Name property. In that case you would just use x:Name=myIdentifier. Have a look at the second line of code below. The following button created in XAML doesn’t have a name.

Once again, the two code examples below are the C# example and the XAML example.

Button btn = new Button();
<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 does however have a Name property. The rules then might be: Use Name unless you have to use x:Name.

<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.

Series Navigation<< C# XAML Introduction Part 3 Type ConvertersC# XAML Combo Boxes >>