#157 – You Can Set Standard CLR Properties from XAML
December 16, 2010 1 Comment
If you create a custom class, you can instantiate instances of that class from XAML by adding the object to a resource dictionary.
<Window.Resources> <m:Person x:Key="perBill" FirstName="William" LastName="Shakespeare" /> </Window.Resources>
You might wonder whether your properties have to be WPF dependency properties in order to set their values from XAML. It turns out that the properties on the custom class do not have to be dependency properties in order to be set from XAML. They can be standard CLR properties.
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public Person() { } }
However, you can’t use the binding syntax to bind another object’s properties unless your custom Person object properties were implemented as Dependency Properties.