What does it mean then to declare nested xaml elements inside our simple class then?


<custom :SimpleBase x:Class="TestType"

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.

    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"
    <sys :String>Test</sys>

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.

see also