O'Reilly logo

XAML Unleashed by Adam Nathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Naming XAML Elements

The XAML language namespace has a Name keyword that enables you to give any element a name. For the simple OK button that we’re imagining is embedded somewhere inside a Page, the Name keyword can be used as follows:

<Button x:Name= "okButton " >OK</Button>

With this in place, you can update the preceding C# code to use Page’s FindName method that searches its children (recursively) and returns the desired instance:

Page page = null;using (FileStream fs =  new FileStream("MyPage.xaml", FileMode.Open, FileAccess.Read)){  // Get the root element, which we know is a Page  page = (Page)XamlReader.Load(fs);}// Grab the OK button, knowing only its name Button okButton = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required