Search code examples
c#wpfxamlmarkup-extensions

Question about this xaml markup extension


I'm trying to understand what does the markup extension for the x:Key attribute below do and what kind of markup extension is it?

<Window x:Class="App1.Window1" xmlns:dxg="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

<DataTemplate x:Key="{dxg:Example ResourceKey=Example}">
    <dxg:TextEdit Text="123/>
</DataTemplate>

</Window>

Thanks.


Solution

  • Well, that example won't do anything - rather, it will fail, because isn't a markup extension named Example in the WPF namespace.

    But if there were a markup extension named Example, what it would do is instantiate an ExampleMarkupExtension object, set its ResourceKey property, and then call its ProvideValue method, which would return an object that would be used as the key for the item being added to the resource dictionary.

    Without more context, it's hard to know what the example you've provided is intended to show. I'd guess that the concepts being demonstrated are a) that the key to a resource dictionary can be any object, not just a string, and b) that you can use a markup extension to generate that key. A real example:

    <DataTemplate x:Key="{x:Type TextBox}">
    

    which adds a DataTemplate with a key of typeof(TextBox) to the resource dictionary.