WPF矢量图形作为DrawingImage StaticResource从DataTemplate不可见

时间:2018-02-15 16:57:44

标签: wpf datatemplate vector-graphics staticresource

在我们的WPF应用程序中,我们有一堆按钮图标的矢量图形。我们在按钮样式上将ImageSource设置为StaticResource,如下所示。矢量本身是使​​用DrawingImage创建的。

一切正常,除非按钮位于DataTemplate中,用于绑定到列表的任何类型的ItemsControl。在这种情况下,按钮功能完美,但DrawingImage不可见。

<DrawingImage x:Key="MyIconVector}" x:Shared="False">
    <DrawingImage.Drawing>
        <GeometryDrawing>
            <GeometryDrawing.Geometry>
                <PathGeometry Figures="M 0,8 L 8,0 L 16,8" FillRule="EvenOdd"/>
            </GeometryDrawing.Geometry>
            <GeometryDrawing.Pen>
                <Pen Thickness="1.5" Brush="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=controls:ImageButton}, FallbackValue=Black}" LineJoin="Round" EndLineCap="Round" StartLineCap="Round"/>
            </GeometryDrawing.Pen>
        </GeometryDrawing>
    </DrawingImage.Drawing>
</DrawingImage>
<Style x:Key="MyButton" TargetType="{x:Type controls:ImageButton}" BasedOn="{StaticResource ImageOnlyButton}">
    <Setter Property="ImageMaxHeight" Value="20"/>
    <Setter Property="ImageMaxWidth" Value="24"/>
    <Setter Property="Width" Value="48"/>
    <Setter Property="ImageSource" Value="{StaticResource MyIconVector}"/>
</Style>

然后一个简单的列表框控件如下,按钮将为空。

<ListBox ItemsSource="{Binding MyList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <controls:ImageButton Style="{StaticResource MyButton}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

1 个答案:

答案 0 :(得分:0)

事实证明,上面的示例向量并不能真正反映我的情况。为了简洁起见,我试图抓住一个更简单的例子。想象一下,使用PriorityBindings看起来更像是这样。

事实证明我们遇到了这些PriorityBindings的问题。删除它们并使用标准的Pen装订(如我原来的帖子中所述),解决了这个问题。

<DrawingImage x:Key="ExpandIcon" x:Shared="False">
    <DrawingImage.Drawing>
        <GeometryDrawing>
            <GeometryDrawing.Geometry>
                <PathGeometry Figures="M 0,9.5 L 0,16 6.5,16 M 0,16 L 6.5,9.5 M 9.5,0 L 16,0 16,6.5 M 16,0 L 9.5,6.5" FillRule="EvenOdd"/>
            </GeometryDrawing.Geometry>
            <GeometryDrawing.Pen>
                <Pen Thickness="1.5" LineJoin="Round" EndLineCap="Round" StartLineCap="Round">
                    <Pen.Brush>
                        <PriorityBinding>
                            <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=controls:ImageButton}" Path="Foreground"/>
                            <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=controls:ImageToggleButton}" Path="Foreground" FallbackValue="Black"/>
                        </PriorityBinding>
                    </Pen.Brush>
                </Pen>
            </GeometryDrawing.Pen>
        </GeometryDrawing>
    </DrawingImage.Drawing>
</DrawingImage>
相关问题