如何让StyleImage脱离风格?

时间:2016-03-17 22:23:39

标签: wpf xaml

我将DrawingBrush放置如下 -

             <DrawingBrush Viewbox="0,0,39.125,39.125" ViewboxUnits="Absolute">
                <DrawingBrush.Drawing>
                    <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=uiEntityViews:NodeCanvasView}, Path=MyColor}" 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>

有了这个,我可以从DrawingBursh获得如下图像 -

            if (resourceObject is DrawingBrush)
            {
                DrawingBrush iconBrush = resourceObject as DrawingBrush;
                DrawingImage image = new DrawingImage(iconBrush.Drawing);
                resourceObject = image;
            }

现在,我在下面的样式中添加了DrawingBrush -

 <Style x:Key="MY_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:NodeCanvasView}, Path=MyColor}" 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>

在这种情况下,我不知道如何从这个样式中获取DrawingImage,如下所示 -

            if (resourceObject is Style)
            {
                Style iconStyle = resourceObject as Style;
                //How to get drawingImage out of iconStyle                   
                // DrawingImage image = new DrawingImage(iconStyle.DrawingBrush);
                //resourceObject = iconStyle;
            }

1 个答案:

答案 0 :(得分:1)

 if (resourceObject is Style)
 {
     Style iconStyle = resourceObject as Style;

     if (iconStyle != null)
     {
         foreach (var setter in iconStyle.Setters.OfType<Setter>())
         {
             if (setter.Value is DrawingBrush)
             {
                  DrawingBrush iconBrush = (DrawingBrush)setter.Value;
                  DrawingImage image = new DrawingImage(iconBrush.Drawing);
                  resourceObject = image;
                  break;
             }                  
         }
     }
}