如何使用WPF制作一个小的图像按钮

如何使用WPF制作一个小的图像按钮,wpf,Wpf,我正在尝试创建一个小的21x21按钮,上面有一个图像。我想要一个普通按钮的所有正常行为/效果,除了图像应该占据按钮的大部分面部 我创建了一个按钮: <Button Grid.Column="2" Height="21" Width="21" HorizontalAlignment="Center" VerticalAlignment="Center"> <Image Source="{StaticResource CloseButton}" Heig

我正在尝试创建一个小的21x21按钮,上面有一个图像。我想要一个普通按钮的所有正常行为/效果,除了图像应该占据按钮的大部分面部

我创建了一个按钮:

<Button Grid.Column="2" Height="21" Width="21" HorizontalAlignment="Center" VerticalAlignment="Center">
   <Image Source="{StaticResource CloseButton}" 
          Height="21" Width="21" 
          HorizontalAlignment="Center" VerticalAlignment="Center">
    </Image>
</Button>
图像最终被压扁,看起来像是因为按钮的默认边距

我尝试了正确的渲染,但是我失去了鼠标悬停的效果,点击了按钮的外观

图像本身是21x21


我怎样才能得到我想要的效果呢?

试试这个方法,我有图像和鼠标悬停属性

 <Button Width="25" Height="25">
            <Image Width="21" Height="21" HorizontalAlignment="Center">
                <Image.Source>
                    <BitmapImage UriSource="../images/icon.png"/>
                </Image.Source>
            </Image>
  </Button>

在回答中,您附加的ControlTemplate目标类型丢失。应该是:

<ControlTemplate TargetType="{x:Type Button}">
    ...
</ControlTemplate>
它表示您的控件模板具有您在本例中指定的控件行为-按钮


希望有帮助。

@Clemens是的,你是正确的,因为图像的高度和宽度为21 x 21,所以没有效果。