繁体   English   中英

如何在DataGrid单元格模板中设置图像大小的样式?

[英]How to style image size inside DataGrid cell template?

我需要使DataGrid中的所有图像具有相同的大小。 在许多地方,图像都位于项目单元格模板内部。 如何将尺寸样式应用于所有样式?

UPD代码示例:

                    <DataGrid.Columns>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Image Source="Resources/Image1.png"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        // some other columns with text or images
                    </DataGrid.Columns>

您可以使用TargetType of Image创建样式来控制尺寸,然后将其应用于所有图像。

以下内容在您的资源字典中

    <Style x:Key="smallImageStyleKey" TargetType="Image"> 
        <Setter Property="Width" Value="32" />
        <Setter Property="Height" Value="32" />
    </Style>

然后修改图像的XAML,使其看起来像

<Image Source="Resources/Image1.png" Style="{StaticResource smallImageStyleKey}"/>

我个人将每个图像放在一个ViewBox然后将样式应用于该图像。

理想情况下,您应该首先在适当的图像编辑程序中调整所有图像的大小,尽管这样做比在WPF中调整图像的大小要好得多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM