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

时间:2011-03-26 08:48:42

标签: c# .net wpf binding datatemplate

我需要让我的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>

1 个答案:

答案 0 :(得分:1)

您可以创建一个TargetType为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中调整它们的大小。

相关问题