【发布时间】:2020-06-21 20:33:54
【问题描述】:
我有 5 个 WPF GeometryDrawing 对象,它们被放置在资源字典中,因为它们可以在不同的视图中使用。 GeometryDrawing 的 Brush 属性应绑定到具体视图模型中的 Enum 属性。 5 个 GeometryDrawing 对象中的任何一个都可以放置到 WPF Image 中(见下文),它取决于另一个 Enum 属性。如何实现?请让我知道是否需要任何其他说明。谢谢。
<Image Source="Should be any of [image1, image2, image3, image4, image4]"></Image>
资源字典:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Geometry x:Key="geom1">#Some geometry</Geometry>
<DrawingGroup x:Key="group1">
<GeometryDrawing Brush="{Binding Path=Background,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}" Geometry="{StaticResource geom1}" />
</DrawingGroup>
<DrawingImage x:Key="image1" Drawing="{StaticResource group1}" />
<Geometry x:Key="geom2">#Some geometry</Geometry>
<DrawingGroup x:Key="group2">
<GeometryDrawing Brush="{Binding Path=Background,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}" Geometry="{StaticResource geom2}" />
</DrawingGroup>
<DrawingImage x:Key="image2" Drawing="{StaticResource group2}" />
<Geometry x:Key="geom3">#Some geometry</Geometry>
<DrawingGroup x:Key="group3">
<GeometryDrawing Brush="{Binding Path=Background,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}" Geometry="{StaticResource geom3}" />
</DrawingGroup>
<DrawingImage x:Key="image3" Drawing="{StaticResource group3}" />
<Geometry x:Key="geom4">#Some geometry</Geometry>
<DrawingGroup x:Key="group4">
<GeometryDrawing Brush="{Binding Path=Background,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}" Geometry="{StaticResource geom4}" />
</DrawingGroup>
<DrawingImage x:Key="image4" Drawing="{StaticResource group4}" />
<Geometry x:Key="geom5">#Some geometry</Geometry>
<DrawingGroup x:Key="group5" >
<GeometryDrawing Brush="{Binding Path=Background,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}"{StaticResource geom5}" />
</DrawingGroup>
<DrawingImage x:Key="image5" Drawing="{StaticResource group5}" />
<DataTemplate x:Key="Icon1">
<Image Source="{StaticResource image1}"/>
</DataTemplate>
<DataTemplate x:Key="Icon2">
<Image Source="{StaticResource image2}"/>
</DataTemplate>
<DataTemplate x:Key="Icon3">
<Image Source="{StaticResource image3}"/>
</DataTemplate>
<DataTemplate x:Key="Icon4">
<Image Source="{StaticResource image4}"/>
</DataTemplate>
<DataTemplate x:Key="Icon5">
<Image Source="{StaticResource image5}"/>
</DataTemplate>
</ResourceDictionary>
现在我在 View 中使用它(为了简单起见,ContentControl 中的 Background="Green",以后可以很容易地绑定它。):
<ContentControl Background="Green">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource Icon1}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MainWindow1, Path=Id}" Value="0">
<Setter Property="ContentTemplate" Value="{StaticResource Icon2}" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=MainWindow1, Path=Id}" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource Icon3}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
在设计时图像完美显示,但在运行时图像未呈现。
链接到回购Sample project
【问题讨论】:
-
我认为您将需要使用数据模板来执行此操作。 Drawinggroup 是可冻结的,因此如果您将其放入资源字典中,它会在您取出时被冻结。您需要克隆或解冻它以更改任何属性。你可以证明这很容易。给那些刷子。合并您的资源字典。使用 application.current.resources["group5"] 或其他方法将其中一个组取出。看看它是否被冻结或只是尝试设置画笔看看会发生什么。
-
@Andy 感谢您的回复。我几乎解决了我的问题,但是现在图像没有在运行时呈现,而在设计时预览是可以的。请查看更新的代码。