拉伸填充容器的路径

时间:2015-11-18 17:32:48

标签: wpf xaml resources wpf-controls resourcedictionary

我有一个像这样定义的资源:

<Canvas x:Key="export"
        Width="48"
        Height="48">
    <Path Fill="{DynamicResource CurrentColor}"
          Data="M23,12L19,8V11H10V13H19V16M1,18V6C1,4.89 1.9,4 3,4H15A2,2 0 0,1 17,6V9H15V6H3V18H15V15H17V18A2,2 0 0,1 15,20H3A2,2 0 0,1 1,18Z" />
</Canvas>

我正在使用它:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Button Margin="10,5,10,10"
                Width="Auto"
                Height="Auto">
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <Rectangle Width="48"
                                Height="48"
                                Fill="{DynamicResource CurrentColor}">
                        <Rectangle.OpacityMask>
                            <VisualBrush Stretch="Fill"
                                            Visual="{Binding}" />
                        </Rectangle.OpacityMask>
                    </Rectangle>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </DataTemplate>
</ItemsControl.ItemTemplate>

可以看出我正在尝试将矩形大小设置为48x48,但资源中的路径数据坐标被定义为将其绘制为24x24。无论我尝试什么,我都无法拉伸资源来填充按钮。怎么做到呢?

如果需要,我可以更改按钮实现。我只需要能够以某种颜色显示按钮中的图标。

2 个答案:

答案 0 :(得分:1)

尝试使用HorizontalAlignment="Stretch"VerticalAlignment="Stretch"

但是如果设置了WidthHeight,它们将优先于Stretch设置。见Microsoft on HorizontalAlignment

答案 1 :(得分:1)

删除画布并使用Stretch="Uniform"上的Path。如果您想减少Path的高度/宽度,请将其添加到例如。 Grid或仅设置Width的{​​{1}}和Height

Path
相关问题