Aleksey
Aleksey

Reputation: 487

SVG icons in a WPF application

I have converted an SVG image into a PathGeometry and saved it as a static resource in my WPF application. I want to display it as an icon on a MenuItem, so I do it like this:

                    <MenuItem.Icon>
                        <Grid Width="18" Height="18">
                          <Viewbox Stretch="Fill">
                                <Path
                                   Fill="DimGray"
                                   Data="{StaticResource ZoomIn}"
                                 />
                          </Viewbox>
                        </Grid>
                    </MenuItem.Icon>

As I have many MenuItems, and I plan to have more SVG icons, to avoid code duplication I consider creating some UserControl based on the code above that could be utilized like this:

<MenuItem.Icon>
 <uc:MyGeometryContainer
   Width="18"
   Height="18"
   Fill="DimGray"
   Geometry="{StaticResource ZoomIn}"/>
</MenuItem.Icon>

It's important that this control should have the ability to set its Width, Height and color to customize the icon view. I got stuck with it, as UserControl exposes its DependencyProperties for a particular DataContext, which means that I will have to create some data object instead of assigning values directly in XAML. I find it awkward. How would you implement such control?

Upvotes: 1

Views: 3617

Answers (1)

Aleksey
Aleksey

Reputation: 487

Thanks to Clemens' comment, I've exploited Path's Stretch property. The code is now concise, and there's no need for a separate control:

                        <MenuItem.Icon>
                                <Path
                                    Stretch="Fill"
                                    Width="18"
                                    Height="18"
                                    Fill="DimGray"
                                    Data="{StaticResource ZoomIn}"> 
                                </Path>
                       </MenuItem.Icon>

Upvotes: 2

Related Questions