Basic XAML Part 2
What does it mean then to declare nested xaml elements inside our simple class then?
e.g.
<custom :SimpleBase x:Class="TestType" xmlns:custom="clr-namespace:ConsoleApplication1;assembly=" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> Test </custom>
Well, if you try it you will find this isn’t possible. And indeed I also can’t figure out what it might mean. And yet if you open any book on XAML you will find things like <button>Click Me</button>. Hmm.
The answer lies in the definition of the base type (in our case SimpleBase).
In order to be able to assign direct content such as this we must declare a property on the base type and add a [System.Windows.Markup.ContentProperty(“…”)] attribute to it where the … is replaced with the name of the property that should take the content.
[System.Windows.Markup.ContentProperty("PropertyOne")] public class SimpleBase { public string PropertyOne { get; set; } public string PropertyTwo { get; set; } }
Now in xaml we can assign the value PropertyOne using the following syntax:
<custom :SimpleBase x:Class="TestType" xmlns:custom="clr-namespace:ConsoleApplication1;assembly=" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <sys :String>Test</sys> </custom>
Interestingly it still isn’t possible to directly enter the string ‘Test’, only to assign a sys:String object containing Test. Putting Test directly in the xml leads to a compile error.
You must be logged in to post a comment.