这个问题很简单,但是对于从winfrom转过来的来讲,在做事的时候就会被绕进去,言归正传,如何设置一个bUtton的背景图片?如何去掉让人烦的默认选中时的灰色背景?请看如下的描述。问题的来源和解决都在XAML中!
有这样的一个定义,在XAML中定义一个Button:
<Button Margin="438,145,0,0" Name="button1" HorizontalAlignment="Left" Width="130" Height="120" VerticalAlignment="Top" Cursor="Hand" Click="button1_Click" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" Focusable="True"> //define wpf button ControlTemplate </Button>
然后呢就是要在主要的地方加上描述Template的定义和设置,就OK了。如下代码:
<Button.Template> <ControlTemplate TargetType="Button"> <ContentControl Name="b1"> <Image Source="/WpfNameSpace;component/image/1.png"></Image> </ContentControl> </ControlTemplate> </Button.Template>
如果需要动态的改变Button的样式的,就需要加上Triggers了。如下代码:
<ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Content"> <Setter.Value> <Image Source="/WpfNameSpace;component/image/2.png" /> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers>
这样,WPF的背景图片就设置成功了,不会出现烦人的灰色背景了。