虎贲小队程序猿

get along

导航

为Button 添加图片---WPF

在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}" />

posted on 2013-04-17 11:26  jjssl  阅读(418)  评论(0编辑  收藏  举报