Search code examples
wpfxamlpathgeometry

In which controls, other than Button, can WPF Geometry icons be shown without defining Stroke or Fill?


I need to show icons like this:

<Geometry x:Key="Geometry.LeftArrow">M 6.5 0.5 L 7.5 1 L 7.5 0 L 6.5 0.5 z</Geometry>

If I put them in Button they're Ok. However I need to show them in many other places but they're not shown. Sometimes it helps to define Stroke/Fill but this is wrong. I don't want to make up some colors that are hardcoded and look stupid when Theme/Style is changed.

This works:

<Button>
    <Path Data="{StaticResource Geometry.LeftArrow}" Stretch="Uniform" MaxHeight="12"/>
</Button>

This does not:

<Label>
    <Path Data="{StaticResource Geometry.LeftArrow}" Stretch="Uniform" MaxHeight="12"/>
</Label>

So what is the logic here and how can I have just show them let's say next to some TextBox?


Solution

  • I tried this and the Path didn't get a default fill or stroke in a Button.

    My solution would be to define a default Path Style which sets Fill (or Stroke, whichever) to {DynamicResource {x:Static SystemColors.ControlTextBrushKey}}.

    <Style TargetType="Path">
        <Setter 
            Property="Fill" 
            Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" 
            />
    </Style>
    

    A well-designed theme should use those SystemColors brush keys. If a theme isn't well designed, you can add some "shim" resource definitions which define SystemColors brush keys to appropriate theme resources.

    If I wanted to give Path a default fill in Button but not elsewhere, I could do that by defining that default Path style in the resources for the Button style:

    <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Style.Resources>
            <Style TargetType="Path">
                <Setter 
                    Property="Fill" 
                    Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" 
                    />
            </Style>
        </Style.Resources>
    
        <!-- other stuff -->
    </Style>
    

    And now that you've talked to your style and icon people (so jelly; we have to do that stuff for ourselves), it looks like that's the essence of what's happening here.