为Button 添加图片---WPF

时间:2020-12-25 10:52:53

在WPF中,让button显示为一个Image,可以设置其BackGround为Image或者Content为Image,不过这两种方法都有一些问题~~

1、可以把Button的content设置为一个Image,不过图片的周围有个边框,影响美观~

<Button>

<Image Source="" />

</Button>

2、通过设置Background,不过还是有一个边框

3、重写一个ControlTemplate,并用ImagBrush 填充一个Image

<UserControl.Resources>
    <ControlTemplate x:Key="btnup" TargetType="{x:Type Button}">
            <Grid>
                <Rectangle HorizontalAlignment="Right" Width="11" Height="12">
                    <Rectangle.Fill>
                        <ImageBrush   ImageSource="images\ico_up.png" ></ImageBrush>
                    </Rectangle.Fill>
                </Rectangle>
    
            </Grid>
   </ControlTemplate>
  
   <ControlTemplate x:Key="btndown" TargetType="{x:Type Button}">
            <Grid>
                <Rectangle HorizontalAlignment="Right" Width="11" Height="12">
                    <Rectangle.Fill>
                        <ImageBrush ImageSource="images\ico_down.png" ></ImageBrush>
                    </Rectangle.Fill>
                </Rectangle>
            </Grid>
   </ControlTemplate>

</UserControl.Resources>

<Button Template="{StaticResource btnup}" />
<Button Template="{StaticResource btndown}" />