我的WPF应用程序中有以下结构:
的App.xaml:
<Application.Resources>
<ResourceDictionary>
<DrawingImage x:Key="mycion">
<DrawingImage.Drawing>
<GeometryDrawing Brush="{WHAT DO I PUT HERE?}" Geometry="M8,8L56,8 56,13.375 8,13.375 8,8z M8,24L8,18.625 56,18.625 56,24 8,24z M8,34.625L8,29.375 56,29.375 56,34.625 8,34.625z M8,45.375L8,40 56,40 56,45.375 8,45.375z M8,56L8,50.625 56,50.625 56,56 8,56z" />
</DrawingImage.Drawing>
</DrawingImage>
</ResourceDictionary>
</Application.Resources>
Window1.xaml:
<Button HorizontalAlignment="Left" Height="49" Margin="33,36,0,0" VerticalAlignment="Top" Width="115">
<!-- I want this image to be Red. -->
<Image Height="25" Width="42" Source="{StaticResource mycion}"/>
</Button>
<Button HorizontalAlignment="Left" Height="49" Margin="33,136,0,0" VerticalAlignment="Top" Width="115">
<!-- I want this image to be Green. -->
<Image Height="25" Width="42" Source="{StaticResource mycion}"/>
</Button>
问题:
如何允许更改资源的刷色(在我的示例中为红色/绿色)(实现为DrawingImage
的资源字典中的自定义图标)绑定到期望{{1的属性> }}?
基本上我想以某种方式将我的图标在ResourceDictionaries中定义为Geometries,但是能够轻松地将这些图标设置为期望ImageSource
的属性以及更改/设置的能力Brush用于以某种方式在父对象或窗口的XAML中呈现Geometry(因此它向下传播到图标资源)。
注意:我仅使用嵌套在ImageSource
中的Image
作为示例。不需要在图像上设置图标,但更常见的是作为期望Button
的其他元素上的属性的值。
我也对其他可能性持开放态度 - 不只是使用ImageSource
,但我对WPF相对较新,所以不知道如何轻松实现它,因为:
有关此事的任何提示将不胜感激。
答案 0 :(得分:1)
将几何图形存储为资源,因为它是重复(干)的最差部分
<Geometry x:Key="ico">M8,8L56,8 56,13.375 8,13.375 8,8z M8,24L8,18.625 56,18.625 56,24 8,24z M8,34.625L8,29.375 56,29.375 56,34.625 8,34.625z M8,45.375L8,40 56,40 56,45.375 8,45.375z M8,56L8,50.625 56,50.625 56,56 8,56z</Geometry>
并创建每种颜色的DrawingImage(GeometryDrawing与DrawingImage分开以使代码更短)
<GeometryDrawing x:Key="red" Brush="Red" Geometry="{StaticResource ico}" />
<DrawingImage x:Key="redIcon" Drawing="{StaticResource red}"/>
<GeometryDrawing x:Key="green" Brush="Green" Geometry="{StaticResource ico}" />
<DrawingImage x:Key="greenIcon" Drawing="{StaticResource green}"/>
使用
<Button HorizontalAlignment="Left" Height="49" VerticalAlignment="Top" Width="115">
<Image Height="25" Width="42" Source="{StaticResource redIcon}"/>
</Button>
<Button HorizontalAlignment="Left" Height="49" VerticalAlignment="Top" Width="115">
<Image Height="25" Width="42" Source="{StaticResource greenIcon}"/>
</Button>
如果需要很多颜色,最好创建一个使用几何资源并返回所需颜色的DrawingImage的转换器
<小时/> 我正在谈论的转换器的例子:
public class GeometryColorConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// getting geometry from App resources
var g = (Geometry)value;
// set Black as default color
string colorName = (string)parameter ?? "Black";
// parsing color name
var color = (Color)ColorConverter.ConvertFromString(colorName);
// creating Image
return new DrawingImage
{
Drawing = new GeometryDrawing(new SolidColorBrush(color), null, g)
};
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
和用法:
<local:GeometryColorConverter x:Key="paint"/>
<Button HorizontalAlignment="Left" Height="49" VerticalAlignment="Top" Width="115">
<Image Height="25" Width="42"
Source="{Binding Source={StaticResource ico},
Converter={StaticResource paint},
ConverterParameter=Orange}"/>
</Button>