如何给wpf的按钮添加背景图片

如何给wpf的按钮添加背景图片,第1张

1、首先你得打开你的VS2015,没有VS2015的下载安装一个,下载安装方法见一下经验。打开你的VS2015,创建一个WPF窗体程序

2、创建好的WPF窗体程序是这样的。中间的空白区域就是我们要更换的背景。

3、我们要更换的背景简单的就是VS2015系统自带的背景图片。在视图——属性窗口中调图窗体程序的属性,在属性的画笔栏中可以更改窗体程序的背景。

4、然后按键F5调试窗口看一下是否更改。

5、如果我想将WPF程序背景更改为任何一张其他的图片,就用复制、粘贴就可以了。

6、可以看到图片与应用程序的窗体大小不一致,我们只需要调节在图片边缘中部那里调整就行了。

7、修改好的图片就出来了,再按键F5调试运行一下看看效果。

直接右键添加啊,然后在格式那一栏里面设置为所以格式将图片添加进来,最后在代码去写上Image source=“sds111.jpg”就OK啦,我这里是假设图片的号码是sds111.jpg

我提供了不用触发器完成的思路。

首先 我写了一个继承RadioButton的类。主要是加了两个string类型的字段,用来存放RadioButton 两个状态下的图片路径

public class MyRadioButton:RadioButton

{

public string EnableImaUri

{

get { return (string)GetValue(EnableImaUriProperty)}

set { SetValue(EnableImaUriProperty, value)}

}

public static readonly DependencyProperty EnableImaUriProperty = DependencyProperty.Register("EnableImaUri", typeof(string), typeof(MyRadioButton), null)

public string DisEnableImaUri

{

get { return (string)GetValue(DisEnableImaUriProperty)}

set { SetValue(DisEnableImaUriProperty, value)}

}

public static readonly DependencyProperty DisEnableImaUriProperty = DependencyProperty.Register("DisEnableImaUri", typeof(string), typeof(MyRadioButton), null)

}

然后在页面前台放上一个button和radiobutton。思路是通过radiobutton的IsEnabledChanged事件来改变Image的source。由于第一次加载时是不触发这个事件的,所以image还是要在前台写一遍默认路径。

<local:MyRadioButton x:Name="myRbtn1" EnableImaUri="ranbo.jpg" DisEnableImaUri="shafa.jpg" IsEnabledChanged="myRbtn1_IsEnabledChanged">

<Image Width="100" Height="100" Source="/WpfApplication1component/Images/shafa.jpg"/>

</local:MyRadioButton>

<Button Width="50" Height="20" Click="Button_Click" Margin="10"/>

然后在后台处理逻辑:

private void Button_Click(object sender, RoutedEventArgs e)

{

if (myRbtn1.IsEnabled == true)

{

myRbtn1.IsEnabled = false

}

else

{

myRbtn1.IsEnabled = true

}

}

private void myRbtn1_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)

{

MyRadioButton rBtn = sender as MyRadioButton

Image img = rBtn.Content as Image if (img == null) return

if (rBtn.IsEnabled == true)

{

Uri uri = new Uri("/Images/" + rBtn.EnableImaUri, UriKind.Relative)

img.Source = new BitmapImage(uri)

}

else

{

Uri uri = new Uri("/Images/" + rBtn.DisEnableImaUri, UriKind.Relative)

img.Source = new BitmapImage(uri)

}

}

OK了!!!有什么问题欢迎继续提问 ^ ^


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6382812.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-21
下一篇 2023-03-21

发表评论

登录后才能评论

评论列表(0条)

保存