views:

33

answers:

2

Hi,

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"&gt;

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

</Window>

Thanks.

A: 

Hi,

usually the x:Key attribute allows you to reference a resource by key, in this case as the template is not within a ResourceDictionary I'm not sure it has any purpose!

Hope this helps!

Jason
Hi,Let's say the DataTemplate is declared in <Windows.Resources>, but the x:Key attribute is to allow other elements to reference the element to which this attribute is declared for (in this case, the DataTemplate), my question is what does this markup extension do with respect to that.Thanks.
+2  A: 

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.

Robert Rossney