在WPF中重新定义/别名资源?

时间:2009-03-11 11:00:50

标签: wpf resources

有没有办法重新定义/替换现有的SolidColorBrush(或其他任何资源,实际上)?

例证:我在外部ResourceDictionary中有一个画笔,我想通过自己的键而不是原始键来引用它。我不想依赖外部参考,因为实际的刷子将来很容易发生变化。

3 个答案:

答案 0 :(得分:14)

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <SolidColorBrush x:Key="SomeExternalResource">Red</SolidColorBrush>
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <StaticResourceExtension ResourceKey="SomeExternalResource" x:Key="SomeAliasedResource"/>
        </Grid.Resources>

        <Border Background="{StaticResource SomeAliasedResource}"/>
    </Grid>
</Window>
  

我不想依赖于   外部参考,因为实际   刷很容易改变   将来

你仍然会依赖外部资源,而不是在很多地方。

答案 1 :(得分:4)

您可以尝试使用StaticResourceExtension,但在全局资源字典中,这不起作用(奇怪的编译器和运行时错误):

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

    <SolidColorBrush x:Key="StatusColor_OK" Color="#ff32a248" />
    <StaticResourceExtension
        x:Key="AliasKey"
        ResourceKey="StatusColor_Error" />
</ResourceDictionary>

为了克服这个问题,我创建了以下类:

/// <summary>
/// Defines an Alias for an existing resource. Very similar to 
/// <see cref="StaticResourceExtension"/>, but it works in
///  ResourceDictionaries
/// </summary>
public class Alias: System.Windows.Markup.MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        IRootObjectProvider rootObjectProvider = (IRootObjectProvider)
            serviceProvider.GetService(typeof (IRootObjectProvider));
        if (rootObjectProvider == null) return null;
        IDictionary dictionary =  rootObjectProvider.RootObject as IDictionary;
        if (dictionary == null) return null;
        return dictionary[ResourceKey];
    }


    public object ResourceKey { get; set; }
}

用法:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

    <SolidColorBrush x:Key="StatusColor_OK" Color="#ff32a248" />
    <Alias
        x:Key="AliasKey"
        ResourceKey="StatusColor_Error" />
</ResourceDictionary>

答案 2 :(得分:3)

我对Ruedi's解决方案进行了更新。这适用于同一资源字典和应用程序内任何位置的资源。

public class Alias : MarkupExtension
{
    public string ResourceKey { get; set; }

    public Alias()
    {

    }
    public Alias(string resourceKey)
    {
        ResourceKey = resourceKey;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return _ProvideLocalValue(serviceProvider) ?? _ProvideApplicationValue();
    }

    private object _ProvideLocalValue(IServiceProvider serviceProvider)
    {
        var rootObjectProvider = (IRootObjectProvider)
            serviceProvider.GetService(typeof(IRootObjectProvider));
        if (rootObjectProvider == null) return null;
        var dictionary = rootObjectProvider.RootObject as IDictionary;
        if (dictionary == null) return null;
        return dictionary.Contains(ResourceKey) ? dictionary[ResourceKey] : null;
    }

    private object _ProvideApplicationValue()
    {
        var value = Application.Current.TryFindResource(ResourceKey);
        return value;
    }
}

用法与上述类似,但关键是使用Alias作为使用的标记扩展,而不是StaticResource。在应用程序资源堆栈中的某处,定义资源...

<Application x:Class="WPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:system="clr-namespace:System;assembly=mscorlib"
             xmlns:wpf="clr-namespace:WPF"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <system:String x:Key="Text">Display some text.</system:String>
        <system:String x:Key="Other">4</system:String>
        <wpf:Alias x:Key="Alias" ResourceKey="Text"/>
        <wpf:Alias x:Key="Second" ResourceKey="Other"/>
    </Application.Resources>
</Application>

无论你在哪里引用别名......

<Window x:Class="WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpf="clr-namespace:WPF"
        Title="MainWindow" Height="150" Width="300">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Text="{wpf:Alias Alias}"/>
        <TextBlock Text="{wpf:Alias Second}"/>
    </StackPanel>
</Window>

我的解决方案需要引用字符串,但它适用于您想要别名的任何对象。