2

In my xaml page, I have one style and one drawingbrush which are given below -

<Style x:Key="ICON_STYLE" TargetType="Rectangle">
    <Setter Property="Fill">
        <Setter.Value>
            <DrawingBrush Viewbox="0,0,39.125,39.125" ViewboxUnits="Absolute">
                <DrawingBrush.Drawing>
                    <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=uiEntityViews:BaseView}, Path=MeSiteColor}" Geometry="F1M19.5625,0.999954C29.8144,0.999954 38.125,9.31053 38.125,19.5625 38.125,29.8142 29.8143,38.1249 19.5625,38.1249 9.31073,38.1249 1,29.8142 1,19.5625 1,9.31053 9.31064,0.999954 19.5625,0.999954z">
                        <GeometryDrawing.Pen>
                            <Pen DashCap="Square" EndLineCap="Flat" LineJoin="Round" MiterLimit="10" StartLineCap="Flat" Thickness="2">
                                <Pen.Brush>
                                    <LinearGradientBrush EndPoint="0.849422,0.849423" StartPoint="0.150577,0.150578">
                                        <GradientStop Color="#FF657783" Offset="0"/>
                                        <GradientStop Color="White" Offset="0.146"/>
                                        <GradientStop Color="#FF2C4758" Offset="1"/>
                                    </LinearGradientBrush>
                                </Pen.Brush>
                                <Pen.DashStyle>
                                    <DashStyle/>
                                </Pen.DashStyle>
                            </Pen>
                        </GeometryDrawing.Pen>
                    </GeometryDrawing>
                </DrawingBrush.Drawing>
            </DrawingBrush>
        </Setter.Value>
    </Setter>
</Style>




<DrawingBrush x:Key="ICON_BRUSH">
<DrawingBrush.Drawing>
  <DrawingGroup>
    <GeometryDrawing Brush="Gray" Geometry="F1 M0,25 L25,50, 50,25 25,0z">
      <GeometryDrawing.Pen>
        <Pen DashCap="Triangle" EndLineCap="Flat" LineJoin="Bevel" MiterLimit="10" StartLineCap="Flat" Thickness="5">
          <Pen.Brush>
            <LinearGradientBrush>
              <GradientStop Color="Red" Offset="0"/>
              <GradientStop Color="Green" Offset="1"/>
            </LinearGradientBrush>
          </Pen.Brush>
          <Pen.DashStyle>
            <DashStyle/>
          </Pen.DashStyle>
        </Pen>
      </GeometryDrawing.Pen>
    </GeometryDrawing>
  </DrawingGroup>
</DrawingBrush.Drawing>

How do I combine these two into one style. I have tried the following but it did not work.

<Style x:Key="COMBINED_NODE_ICON" TargetType="Rectangle" BasedOn="{StaticResource ICON_STYLE}">
<Setter Property="Fill">
  <Setter.Value>
    <DrawingBrush TileMode="None">
      <DrawingBrush.Drawing>
          <DrawingGroup>
            <DrawingGroup.Transform>
              <TranslateTransform X="0.2" Y="0.2" />
            </DrawingGroup.Transform>
            <GeometryDrawing Brush="{StaticResource ICON_BRUSH}">
              <GeometryDrawing.Geometry>
                <RectangleGeometry Rect="0,0,1,1" />
              </GeometryDrawing.Geometry>
            </GeometryDrawing>
        </DrawingGroup>
      </DrawingBrush.Drawing>
    </DrawingBrush>
  </Setter.Value>
</Setter>

Any help will be highly appreciated. Thanks.

Taterhead
  • 5,763
  • 4
  • 31
  • 40
user109260
  • 878
  • 7
  • 22
  • Instead of `BasedOn` did you try simply to use it as Setter value ``? – dkozl Mar 24 '16 at 22:08
  • If I set the setter Value, it won't let be add anything to – user109260 Mar 24 '16 at 22:41
  • 1
    You can set each property only once per command. What else are you trying to put there? Isn't seeing setter value with `DrawingBrush` your goal? – dkozl Mar 25 '16 at 05:58
  • I want to combine two drawingbrush together, the one is inside ICON_STYLE and the other is ICON_BRUSH. – user109260 Mar 26 '16 at 01:01
  • Does this answer your question? [In WPF, Is there any way to combine two Style for one control?](https://stackoverflow.com/questions/6313236/in-wpf-is-there-any-way-to-combine-two-style-for-one-control) – StayOnTarget Aug 16 '21 at 17:29

1 Answers1

1

This should work (dkozl plus BasedOn):

<Style x:Key="COMBINED_NODE_ICON"
       TargetType="Rectangle"
       BasedOn="{StaticResource ICON_STYLE}">
    <Setter Property="Fill"
            Value="{StaticResource ICON_BRUSH}" />
</Style>

The BasedOn part can be omitted (pure dkozl) as long as your base style (ICON_STYLE) contains nothing but the Fill Setter, since the Fill property is overridden inside style COMBINED_NODE_ICON.

But also your code is working, if you add the last line of

<DrawingBrush x:Key="ICON_BRUSH">
    ...
</DrawingBrush>

and the last line of

<Style x:Key="COMBINED_NODE_ICON" TargetType="Rectangle" BasedOn="{StaticResource ICON_STYLE}">
    ...
</Style>
Pollitzer
  • 1,580
  • 3
  • 18
  • 28
  • This does not work. If I do this, then only ICON_BRUSH is applied and ICON_STYLE goes missing. – user109260 Mar 25 '16 at 17:06
  • Right, as I said, in the current case it's no difference if you use `BasedOn` or not, because ICON_STYLE is ignored as soon as you use the Fill Setter. dkozl has already statet: The Fill property can only be set once. – Pollitzer Mar 25 '16 at 20:53
  • I want to combine two drawingbrush together, the one is inside ICON_STYLE and the other is ICON_BRUSH. If I used BasedOn, ICON_STYLE is ignored and I only got ICON_BRUSH as output. Here, ICON_STYLE gives a circle as output and ICON_BRUSH adds a diamond shape on top of the circle. Thanks. – user109260 Mar 26 '16 at 01:02
  • You want the system to merge two `Fill` `Setter`s, this is not possible. Only one `Fill` `Setter` can be applied at a time; which one is a question of priority. In the case above priority has the `Setter` in style COMBINED_NODE_ICON, the `BasedOn` style is ignored. The merge must be done by yourself, make a new `Brush` and put it in a new style, that's all I can say. – Pollitzer Mar 26 '16 at 08:06