简体   繁体   中英

How to extract Setter value property from Style in Xaml?

I have a style has setter property named "Fill" and has DrawingBrush as its value. I want to extract DrawingBrush from it to use in the viewmodel. Here, is the style I have.

<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:ViewModel}, 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>

I want to extract DrawingBrush as follows -

if (resourceObject is Style)
{
    Style iconStyle = resourceObject as Style;
    var collections = iconStyle.Setters;
    // How to extract iconBrush from collections????
    DrawingBrush iconBrush = ???
}

The following might work:

DrawingBrush iconBrush = GrabFirstDrawingBrushInStyle(resourceObject as Style);

private DrawingBrush GrabFirstDrawingBrushInStyle(Style style)
{
    if (style != null)
    {
        foreach (var setter in style.Setters.OfType<Setter>())
        {
            if (setter.Value is DrawingBrush)
                return (DrawingBrush)setter.Value;
        }
    }
    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM