开发者

How to style image size inside DataGrid cell template?

开发者 https://www.devze.com 2023-02-19 17:13 出处:网络
I need to make all images inside my DataGrid the same size. Images are inside items cell templates, in many places. How to apply si开发者_高级运维ze style to all of them?

I need to make all images inside my DataGrid the same size. Images are inside items cell templates, in many places. How to apply si开发者_高级运维ze style to all of them?

UPD code sample:

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


You could create a style with a TargetType of Image to control your sizes and then apply to all your images.

The following goes in your resource dictionary

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

Then modify the XAML for your images to look like

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

Personally I would put each of the images in a ViewBox and apply the style to that though.

Ideally you should resize all of your images in a proper image editing program first though as that will do a much better job of resizing your images than sizing them in WPF will do though.

0

精彩评论

暂无评论...
验证码 换一张
取 消