Alias or Reference in ResourceDictionary

柔情痞子 提交于 2019-12-29 09:09:32

问题


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.


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/5797454/alias-or-reference-in-resourcedictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!