Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DrawingBrush as Window.Icon?

Tags:

c#

wpf

xaml

I have a DLL that has a ResourceDictionary containing a XAML image:

<DrawingBrush x:Key="imgFoo" ViewboxUnits="Absolute" Viewbox="0,0,128,128">
    <DrawingBrush.Drawing>
        <GeometryDrawing Brush="#FF111111">
            <GeometryDrawing.Pen>
                <Pen LineJoin="Miter" StartLineCap="Square" EndLineCap="Square"/>
            </GeometryDrawing.Pen>

            <GeometryDrawing.Geometry>
                <PathGeometry Figures="M 56.5625 ... 64.03125 45.46875 z" 
                              FillRule="NonZero"/>
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>

Is it possible to use this DrawingBrush as Window.Icon?

using StaticResource or DynamicResource doesn't work:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Icon="{DynamicResource imgFoo}">

The only thing I found is using Window.Resources like this:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Icon="{DynamicResource imgFoo}">
    <Window.Resources>
        <DrawingImage x:Key="imgFoo">
            <DrawingImage.Drawing>
                <GeometryDrawing Brush="#FF111111">
                    <GeometryDrawing.Pen>
                        <Pen LineJoin="Miter" StartLineCap="Square" 
                             EndLineCap="Square"/>
                    </GeometryDrawing.Pen>

                    <GeometryDrawing.Geometry>
                        <PathGeometry Figures="M 56.5625 ... 64.03125 45.46875 z" 
                                      FillRule="NonZero"/>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingImage.Drawing>
       </DrawingImage>
  </Window.Resources>

But this uses only a part of the DrawingBrush from the DLL file and creates duplicate XAML code.

Any suggestions on how I could use that DrawingBrush directly?

like image 573
juFo Avatar asked Feb 02 '26 17:02

juFo


1 Answers

You could split the resource declaration into two parts and declare the GeometryDrawing and the DrawingBrush separately:

<GeometryDrawing x:Key="imgFooDrawing" Brush="#FF111111">
    <GeometryDrawing.Pen>
        <Pen LineJoin="Miter" StartLineCap="Square" EndLineCap="Square"/>
    </GeometryDrawing.Pen>
    <GeometryDrawing.Geometry>
        <PathGeometry Figures="M10,64 L64,10 118,64 64,118Z" FillRule="NonZero"/>
    </GeometryDrawing.Geometry>
</GeometryDrawing>
<DrawingBrush x:Key="imgFoo" ViewboxUnits="Absolute" Viewbox="0,0,128,128"
              Drawing="{StaticResource imgFooDrawing}">
</DrawingBrush>

Now you could directly reuse the GeometryDrawing in a DrawingImage that is used as the Window's Icon:

<Window.Icon>
    <DrawingImage Drawing="{DynamicResource imgFooDrawing}"/>
</Window.Icon>

In case you can't change the Resource DLL, you could bind the DrawingImage's Drawing property to the Drawing property of the DrawingBrush.

This would however require an ugly workaround because you can't use a DynamicResource as binding source. You may set the Tag property of the Window to the DrawingBrush, and then create a RelativeSource/FindAncestor binding:

<Window ... Tag="{DynamicResource imgFoo}">
    <Window.Icon>
        <DrawingImage Drawing="{Binding Tag.Drawing,
            RelativeSource={RelativeSource AncestorType=Window}}"/>
    </Window.Icon>
</Window>
like image 162
Clemens Avatar answered Feb 05 '26 08:02

Clemens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!