Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

wpf - Alias or Reference in ResourceDictionary

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...