WPF 图像按钮格式 [英] WPF Image Button formatting

查看:31
本文介绍了WPF 图像按钮格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建一个带有图像的按钮,它会被放大到比图像大得多的尺寸.

If I create a button with an image inside, it gets blown up to much larger size than the image.

如果我试图限制图像和容器按钮的大小,图像就会被截断:

If i try to constrain the size of image, and container button, the image gets cut off:

<Button Background="Transparent" Width="18" Height="18" Margin="0,0,0,0" Padding="0,0,0,0" BorderBrush="{x:Null}">
    <Image Width="16" Height="16" />
</Button>

原始图像大小为 16x16.这里的按钮是 18x18 的 0 填充,但图像仍然在右/底部被截断.

Native image size is 16x16. Here the button is 18x18 with 0 padding, yet the image still gets cut-off on right/bottom.

我怎样才能使整个按钮与图像的大小完全一致而没有任何截断?

how can i make the entire button be exactly the size of the image w/out any cut off?

推荐答案

WPF 的优点(和诅咒?)在于能够重新模板控件.

The beauty (and curse?) of WPF is the ability to re-template controls.

你可以像下面这样设置按钮的模板(这是徒手的,所以你需要根据自己的喜好稍微调整一下):

You can set the template of the button something like the following (this is free hand, so you will need to tweak it a bit for your tastes):

        <Button x:Name="btn16x16">
            <Button.Template>
                <ControlTemplate>
                    <Border HorizontalAlignment="Center" VerticalAlignment="Center" >
                        <Image Source="pack://siteoforigin:,,,/Resources/SixteenBySixteen.png" 
                               Width="16" 
                               Height="16"/>
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>

这篇关于WPF 图像按钮格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆