如何分配WPF资源等资源的标签 [英] How to assign WPF resources to other resource tags

查看:122
本文介绍了如何分配WPF资源等资源的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是相当模糊的,我可能只是失去了一些东西非常简单。

This is quite obscure, I may just be missing something extremely simple.

案例1

可以说,我创建了一个渐变画笔,像这样在我的< Window.Resources> 部分:

Lets say I create a gradient brush, like this in my <Window.Resources> section:

<LinearGradientBrush x:Key="GridRowSelectedBackBrushGradient" StartPoint="0,0" EndPoint="0,1">
    <GradientStop Color="#404040" Offset="0.0" />
    <GradientStop Color="#404040" Offset="0.5" />
    <GradientStop Color="#000000" Offset="0.6" />
    <GradientStop Color="#000000" Offset="1.0" />
</LinearGradientBrush>

然后很久以后,我想重写 HighlightBrushKey 为DataGrid。我已经基本做到了这样的(可怕的);

Then much later on, I want to override the HighlightBrushKey for a DataGrid. I have basically done it like this (horrible);

<LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                     GradientStops="{Binding Source={StaticResource GridRowSelectedBackBrushGradient}, Path=GradientStops}"
                     StartPoint="{Binding Source={StaticResource GridRowSelectedBackBrushGradient}, Path=StartPoint}"
                     EndPoint="{Binding Source={StaticResource GridRowSelectedBackBrushGradient}, Path=EndPoint}" />

这显然不是引用资源的最光滑的方式。我还提出了以下问题,这几乎是相同的。

This is obviously not the most slick way of referencing a resource. I also came up with the following problem, which is almost identical.

情景2

说我创​​建了两个颜色我的&LT; Window.Resources&GT; 标记,就像这样:

Say I created two colors in my <Window.Resources> markup, like so:

<SolidColorBrush x:Key="DataGridRowBackgroundBrush" Color="#EAF2FB" />
<SolidColorBrush x:Key="DataGridRowBackgroundAltBrush" Color="#FFFFFF" />

后来的后来,我希望提供他们在一个阵列,可以喂ConverterParameter具有约束力,所以我可以提供自定义的转换器与我的静态资源实例:

Then later on, I want to supply them in an Array, which feeds the ConverterParameter on a Binding so I can supply the custom Converter with my static resource instances:

<Setter Property="Background">
    <Setter.Value>
        <Binding RelativeSource="{RelativeSource Mode=Self}" 
                 Converter="{StaticResource BackgroundBrushConverter}">
            <Binding.ConverterParameter>
                <x:Array Type="{x:Type Brush}">
                    <SolidColorBrush Color="{Binding Source={StaticResource DataGridRowBackgroundBrush}, Path=Color}" />
                    <SolidColorBrush Color="{Binding Source={StaticResource DataGridRowBackgroundAltBrush}, Path=Color}" />
                </x:Array>
            </Binding.ConverterParameter>
        </Binding>
    </Setter.Value>
</Setter>

我所做的是试图rereference现有的资源,但在我的努力,我实际上已经重新创建的资源,并绑定的属性,以便它们匹配。再次,这是不理想的。

What I've done is attempt to rereference an existing resource, but in my efforts I've actually recreated the resource, and bound the properties so they match. Again, this is not ideal.

由于我一直打到现在这个问题至少两次,有没有更好的办法?

Because I've now hit this problem at least twice, is there a better way?

谢谢,汤姆

推荐答案

当有人要问这个我不知道。

I was wondering when someone was going to ask about this.

您想在方案1什么是有效地给单个资源的别名。这是很容易的标记只有在你看到它那似乎是显而易见的完成。假设我们有这样的在我们的App.xaml中或其他地方:

What you want to do in Scenario 1 is to effectively give a single resource an "alias." This is easily done by markup that seems obvious only after you see it. Suppose we have this in our App.xaml or somewhere:

<ResourceDictionary>
  <LinearGradientBrush x:Key="GridRowSelectedBackBrushGradient" ... />
</ResourceDictionary>

要包含在其他的ResourceDictionary一个别名,只是:

To include an alias in another ResourceDictionary, just:

<ResourceDictionary>
  <StaticResourceExtension x:Key="{x:Static SystemColors.HighlightBrushKey}"
                           ResourceKey="GridRowSelectedBackBrushGradient" />
</ResourceDictionary>

这看起来在第一ResourceDictionary中的画笔对象,并将相同的对象,以第二的ResourceDictionary下一个新的关键。这也适用于在一个ResourceDictionary中。

This looks up the brush object in the first ResourceDictionary and adds the same object to the second ResourceDictionary under a new key. This also works within a single ResourceDictionary.

有关您的方案2的解决办法也很简单:

For your Scenario 2 the solution is just as simple:

<Binding.ConverterParameter>
  <x:Array Type="{x:Type Brush}">
    <StaticResourceExtension ResourceKey="DataGridRowBackgroundBrush" />
    <StaticResourceExtension ResourceKey="DataGridRowBackgroundAltBrush" />
  </x:Array>
</Binding.ConverterParameter>

再次通过的ResourceKey 直接添加到找到的实际对象刷[] 阵列。没有新的创建。

Again, the actual Brush objects found via the ResourceKey are added directly to the Brush[] array. No new Brush is created.

我觉得我们都习惯了使用 StaticResourceExtension 与标记扩展语法(如 {的StaticResource XYZ} ),很容易忘记,它也可以与常规的元素语法使用。

I think we're all so used to using StaticResourceExtension with markup extension syntax (eg {StaticResource Xyz}) that it's easy to forget that it can also be used with regular element syntax as well.

这篇关于如何分配WPF资源等资源的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆