2

I'm looking for a way to essentially give an item in a ResourceDictionary multiple keys. Is there a way to do this?

<DataTemplate x:Key="myTemplate" ... />
<AliasedResource x:Key="myTemplateViaAlias" Target="myTemplate" OR_Target={StaticResource myTemplate} />

When I call TryFindResource("myTemplateViaAlias") I want to get myTemplate out. I suppose I could create an AliasedResource class myself and dereference it in code when I get it out of the dictionary, but I'd rather not do that if there's a built-in way.

default.kramer
  • 5,943
  • 2
  • 32
  • 50

2 Answers2

4

You can pipe resources through dynamic resources (note that crappy UI designers won't like it)

<Color x:Key="color">Red</Color>
<DynamicResource x:Key="mycolor" ResourceKey="color"/>
<Rectangle Width="100" Height="100">
    <Rectangle.Fill>
        <SolidColorBrush Color="{StaticResource color}"/>
    </Rectangle.Fill>
</Rectangle>
<Rectangle Width="100" Height="100">
    <Rectangle.Fill>
        <SolidColorBrush Color="{StaticResource mycolor}"/>
    </Rectangle.Fill>
</Rectangle>
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Perfect, thanks. Now I just have to wait 8 minutes to accept. – default.kramer Apr 26 '11 at 23:06
  • Hmm, never tried data temples in dynamic resources, since the control doesn't know which data types it will get, how can it use a specific dynamic resource? – Danny Varod Apr 26 '11 at 23:10
  • For **UI** resources, dynamic resources do work in Blend. Just won't load by default without adding the dictionary. – Danny Varod Apr 26 '11 at 23:11
  • @Danny Varod: I do not understand what you mean by your first comment... (When i said crappy UI designers i obviously meant Visual Studio) – H.B. Apr 26 '11 at 23:12
  • Question had DataTemplate in it, usually used to define how a specific data type is represented in UI. Since UI can be binded to multiple data types, calling a data template by name is problematic in many cases (unless UI is designed for one data type only). However, for styling, as in your answer's example, this does make sense. – Danny Varod Apr 26 '11 at 23:18
  • @Danny Varod: I suspected that you meant that, but the asker already had named the DataTemplate, so this was no concern of mine. – H.B. Apr 26 '11 at 23:21
0

There is no way that I know of to do this.

However, if your goal is multiple templates for multiple controls, you may be going at this the wrong way.

You could have multiple resources (data templates and dynamic resources) with the same globally named key, or default key (control class type) for styles, then load a different resource dictionary file into every control/region/tab/window, during run time.

Danny Varod
  • 17,324
  • 5
  • 69
  • 111