简体   繁体   English

WPF应用程序中的SVG图标

[英]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. 我已将SVG图像转换为PathGeometry,并将其另存为WPF应用程序中的静态资源。 I want to display it as an icon on a MenuItem, so I do it like this: 我想将其显示为MenuItem上的图标,所以我这样做是这样的:

                    <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: 由于我有许多MenuItems,并且我打算有更多的SVG图标,为避免代码重复,我考虑根据上述代码创建一些UserControl,以便像这样使用:

<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. 我陷入了困境,因为UserControl为特定的DataContext公开了其DependencyProperties,这意味着我将不得不创建一些数据对象,而不是直接在XAML中分配值。 I find it awkward. 我觉得很尴尬。 How would you implement such control? 您将如何实施这种控制?

Thanks to Clemens' comment, I've exploited Path's Stretch property. 感谢Clemens的评论,我利用了Path的Stretch属性。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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