WPF Embedded Resources

Constant data can be embedded in XAML.

<Window.Resources>
  <t1:collectionType x:key="listName">
    <t2:itemType prop="value" .../>
    <t2:itemType prop="value" .../>
    ....
  </t1:collectionType>
  <t3:instancetype x:key="itemName" prop="value..."/>
</Window.Resources>

The data is bound to a property of a control using:

prop="{StaticResource key}"

Where key is the x:key of the item in the resource. In code the resource can be accessed using the FindResource method, again using the x:key.

Leave a Reply