Redefining color values in consumer projects

≯℡__Kan透↙ 提交于 2019-12-12 12:49:00

问题


What is the correct way of overriding color resources defined in WPF Controls Library project?

Say I have project A that is a WPF Controls Library having the following two color resources defined:

<Color x:Key="Color1">WhiteSmoke</Color>
<Color x:Key="Color2">LightGray</Color>

and a Brush created from these colors:

<LinearGradientBrush x:Key="{ComponentResourceKey {x:Type local:MyControl}, MyControlBackground}">
  <GradientStop Offset="0" Color="{StaticResource Color1}" />
  <GradientStop Offset="1" Color="{StaticResource Color2}"/>
</LinearGradientBrush>

One of the controls defined in the library uses this brush resource like this:

<UserControl>
  <TabControl SelectedIndex="0">
    <TabItem Header="Information">
      <Grid>
      ...
      <ScrollViewer>
        <Grid>
        ...
          <Rectangle Fill="{StaticResource {ComponentResourceKey {x:Type local:MyControl}, MyControlBackground}}" />
          ...

So far so good. Now project B (a WPF application) and project C (a Visual Studio extension) need to use this control. Both are referencing project A. The only issue is that these two projects need to define different brushes for the control background.

How should I achieve this? Is there a way to redefine Color1 and Color2 in consumer projects B and C? Obviously I cannot do this by accessing the Rectangle (it is so deep down the hierarchy). Also I cannot use a Style here, since there could be other Rectangles in the hierarchy.

What's my way out?

来源:https://stackoverflow.com/questions/42858662/redefining-color-values-in-consumer-projects

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