问题
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 Rectangle
s in the hierarchy.
What's my way out?
来源:https://stackoverflow.com/questions/42858662/redefining-color-values-in-consumer-projects