zoukankan      html  css  js  c++  java
  • WPF Button的背景图片设置

    这个问题很简单,但是对于从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的背景图片就设置成功了,不会出现烦人的灰色背景了。

  • 相关阅读:
    python3 bs4库
    python3 requests 库学习
    thinkPHP远程代码执行漏洞
    centos 数据库忘记密码
    centos 安装 nfs
    中间人工具---MITMF
    微信公众号授权获取codequ
    vue element 倒计时
    vue常见登陆注册按钮切换效果
    VUE3.0重新定向
  • 原文地址:https://www.cnblogs.com/tymonyang/p/WPF.html
Copyright © 2011-2022 走看看