这个问题很简单,但是对于从winfrom转过来的来讲,在做事的时候就会被绕进去,言归正传,如何设置一个bUtton的背景图片?如何去掉让人烦的默认选中时的灰色背景?请看如下的描述。问题的来源和解决都在XAML中!

有这样的一个定义,在XAML中定义一个Button:

<Button Margin="438,145,0,0" Name="button1" HorizontalAlignment="Left" Width="" Height=""
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的背景图片就设置成功了,不会出现烦人的灰色背景了。

WPF Button的背景图片设置的更多相关文章

  1. WPF之路二: button添加背景图片点击后图片闪烁问题

    在为button添加背景图片的时候,点击后发现图片闪烁,我们仔细观察,其实Button不仅仅只是在点击后会闪烁,在其通过点击或按Tab键获得焦点后都会闪烁,而通过点击其他按钮或通过按Tab键让Butt ...

  2. Android TextView背景颜色与背景图片设置

    Android TextView 背景颜色与背景图片设置,android textview 控件,android textview 背景, android textview 图片,android te ...

  3. JAVA GUI学习 - 窗体背景图片设置方法:重写paintComponent(Graphics g)方法

    public class BackgroundImage extends JFrame { public BackgroundImage() { this.setTitle("窗体背景图片设 ...

  4. 【转】UGUI之用脚本动态的改变Button的背景图片 和 颜色

    http://blog.csdn.net/u014771617/article/details/45102701 public Button button;void Start(){ColorBloc ...

  5. css样式背景图片设置缩放

    一.背景颜色图片平铺 background-color 背景颜色 background-image 背景图片地址 background-repeat 是否平铺 默认是平铺 background-pos ...

  6. WPF学习笔记——为BUTTON添加背景图片

    首先要肯定,代码: <Style x:Key="UserItemButton" TargetType="Button"> <Setter Pr ...

  7. C#(winform)为button添加背景图片

    1.既然是添加背景图片 所以这里应该使用 Button.BackgroudImage = "" ;来设置图片 而不应该使用  Button.Image = "" ...

  8. C#(winform)为button添加背景图片,并去掉各种边框

    1.既然是添加背景图片 所以这里应该使用 Button.BackgroudImage = "" ;来设置图片 而不应该使用  Button.Image = "" ...

  9. 【CSS】css网页背景图片设置

    刚学CSS,了解了下网页背景图设置,顺便记录下. 下面主要是实现背景图位置保持不变,即不随滚动条动而动的功能. body { background-image:url(images/bck.png); ...

随机推荐

  1. 批处理DataTable

    DataTable dt = CreateTable(); SqlConnection conn = new SqlConnection("Data Source=.;Initial Cat ...

  2. Jfinal学习笔记

    实习期间,公司使用jfinal做开发,所以就学习了这个框架 Jfinal作为一个极速ORM,使用上非常方便,核心源代码1万多行,压缩后只有200多KB. 从入口开始 作为一个web项目,首先解读web ...

  3. 树形菜单 jsTree 使用方法

    jsTree版本:3.0.4 在ASP.NET MVC中使用jsTree Model: public class Department { public int Id { get; set; } pu ...

  4. 接收POst数据流数据

    var data = "";                using (StreamReader readStream = new StreamReader(context.Re ...

  5. jboss加密敏感信息

    默认情况下,我们配置在domain.xml或host.xml文件中的信息都是明文,对一些敏感信息就显得安全性不够,可以使用jboss提供的vault机制来进行加密 下面的内容来自 http://www ...

  6. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

  7. ASP多行多列显示代码

    <table width="98%" border="0" align="center"> <tr> <% S ...

  8. asp.net post方法;对象转json

    [System.Web.Services.WebMethod()]     public static string GetPoints(string userId)     {         st ...

  9. HDU(搜索专题) 1000 N皇后问题(深度优先搜索DFS)解题报告

    前几天一直在忙一些事情,所以一直没来得及开始这个搜索专题的训练,今天做了下这个专题的第一题,皇后问题在我没有开始接受Axie的算法低强度训练前,就早有耳闻了,但一直不知道是什么类型的题目,今天一看,原 ...

  10. linux中vsftpd登陆慢卡问题解决方法

    1.修改服务器上的/etc/resolv.conf令其内容只有类似 nameserver 192.81.133.229 nameserver 114.114.114.114 出现问题的机器的resol ...