我在ResourceDictionary
中使用xaml
的Visual Studio 2022 Image Library
图标。所有我必须做的,然后,是给予一个关键,我可以使用它们作为WPF控件的内容:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Rectangle x:Key="ProcessIcon" Width="16 " Height="16">
<Rectangle.Resources>
<SolidColorBrush x:Key="canvas" Opacity="0" />
<SolidColorBrush x:Key="light-defaultgrey" Color="#212121" Opacity="1" />
</Rectangle.Resources>
<Rectangle.Fill>
<DrawingBrush Stretch="None">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup>
<GeometryDrawing Brush="{DynamicResource canvas}" Geometry="F1M16,16H0V0H16Z" />
</DrawingGroup>
<DrawingGroup>
<GeometryDrawing Brush="{DynamicResource light-defaultgrey}" Geometry="F1M8,5V4H6.949a2.473,2.473,0,0,0-.365-.876l.745-.745-.707-.707-.745.745A2.469,2.469,0,0,0,5,2.051V1H4V2.051a2.469,2.469,0,0,0-.877.366l-.745-.745-.707.707.745.745A2.473,2.473,0,0,0,2.051,4H1V5H2.051a2.473,2.473,0,0,0,.365.876l-.745.745.707.707.745-.745A2.469,2.469,0,0,0,4,6.949V8H5V6.949a2.469,2.469,0,0,0,.877-.366l.745.745.707-.707-.745-.745A2.473,2.473,0,0,0,6.949,5ZM4.5,6A1.5,1.5,0,1,1,6,4.5,1.5,1.5,0,0,1,4.5,6Z" />
<GeometryDrawing Brush="{DynamicResource light-defaultgrey}" Geometry="F1M13.937,10.633a3.271,3.271,0,0,0-.1-.784l1.242-.514-.383-.924-1.244.515A3.314,3.314,0,0,0,12.34,7.819l.515-1.245-.923-.383-.515,1.242a3.007,3.007,0,0,0-1.568,0L9.335,6.191l-.924.383.515,1.245A3.311,3.311,0,0,0,7.819,8.926L6.575,8.411l-.383.924,1.241.514a3.007,3.007,0,0,0,0,1.568l-1.241.515.383.923,1.244-.515a3.314,3.314,0,0,0,1.107,1.108l-.515,1.243.924.383.514-1.241a2.983,2.983,0,0,0,1.569,0l.514,1.241.923-.383-.515-1.243a3.307,3.307,0,0,0,1.108-1.108l1.244.515.383-.923-1.242-.515A3.278,3.278,0,0,0,13.937,10.633Zm-5.608,0a2.3,2.3,0,1,1,2.3,2.305A2.307,2.307,0,0,1,8.329,10.633Z" />
</DrawingGroup>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
</ResourceDictionary>
<Button Command="{Binding ...}">
<StackPanel Orientation="Horizontal">
<Label Content="{StaticResource ProcessIcon}" Margin="0 -5"/>
<TextBlock Text="Title" />
</StackPanel>
</Button>
这只适用于一个按钮。当在第二个按钮上使用相同的图标时,第一个按钮不再显示图标。
我发现,这是因为图标是FrameworkElement
,只能有一个父.解决方案应该是将其标记为x:Shared=False
:WPF: Can use StaticResource only once
但这在这里行不通,我不知道为什么。因为ResourceDictionary
?因为图标不是图像?
我也不知道,如何将Rectangle
转换为图像。这是微软推出的xaml.
更新:基于emoacht's answer,我可以这样使用它:
<Button Command="{Binding ...}">
<StackPanel Orientation="Horizontal">
<Label Background="{StaticResource ProcessIcon}" Width="16" Height="16" Margin="5 0" />
<TextBlock Text="Title" />
</StackPanel>
</Button>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<VisualBrush x:Key="ProcessIcon">
<VisualBrush.Visual>
<Rectangle Width="16 " Height="16">
<Rectangle.Resources>
<SolidColorBrush x:Key="canvas" Opacity="0" />
<SolidColorBrush x:Key="light-defaultgrey" Color="#212121" Opacity="1" />
</Rectangle.Resources>
<Rectangle.Fill>
<DrawingBrush Stretch="None">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup>
<GeometryDrawing Brush="{DynamicResource canvas}" Geometry="F1M16,16H0V0H16Z" />
</DrawingGroup>
<DrawingGroup>
<GeometryDrawing Brush="{DynamicResource light-defaultgrey}" Geometry="F1M8,5V4H6.949a2.473,2.473,0,0,0-.365-.876l.745-.745-.707-.707-.745.745A2.469,2.469,0,0,0,5,2.051V1H4V2.051a2.469,2.469,0,0,0-.877.366l-.745-.745-.707.707.745.745A2.473,2.473,0,0,0,2.051,4H1V5H2.051a2.473,2.473,0,0,0,.365.876l-.745.745.707.707.745-.745A2.469,2.469,0,0,0,4,6.949V8H5V6.949a2.469,2.469,0,0,0,.877-.366l.745.745.707-.707-.745-.745A2.473,2.473,0,0,0,6.949,5ZM4.5,6A1.5,1.5,0,1,1,6,4.5,1.5,1.5,0,0,1,4.5,6Z" />
<GeometryDrawing Brush="{DynamicResource light-defaultgrey}" Geometry="F1M13.937,10.633a3.271,3.271,0,0,0-.1-.784l1.242-.514-.383-.924-1.244.515A3.314,3.314,0,0,0,12.34,7.819l.515-1.245-.923-.383-.515,1.242a3.007,3.007,0,0,0-1.568,0L9.335,6.191l-.924.383.515,1.245A3.311,3.311,0,0,0,7.819,8.926L6.575,8.411l-.383.924,1.241.514a3.007,3.007,0,0,0,0,1.568l-1.241.515.383.923,1.244-.515a3.314,3.314,0,0,0,1.107,1.108l-.515,1.243.924.383.514-1.241a2.983,2.983,0,0,0,1.569,0l.514,1.241.923-.383-.515-1.243a3.307,3.307,0,0,0,1.108-1.108l1.244.515.383-.923-1.242-.515A3.278,3.278,0,0,0,13.937,10.633Zm-5.608,0a2.3,2.3,0,1,1,2.3,2.305A2.307,2.307,0,0,1,8.329,10.633Z" />
</DrawingGroup>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
</VisualBrush.Visual>
</VisualBrush>
</ResourceDictionary>
1条答案
按热度按时间5lhxktic1#
您可以从任何Visual(包括Rectangle)创建
VisualBrush
,并将其存储在参考资料中。然后你可以像其他画笔一样使用它作为图标图像。