《WPF图片按钮》
一、 说明
因为在做WPF项目的时候用到了带图片的按钮,所以就讲一下在VS中的WPF如何弄一个有背景图片的按钮。
二、 准备工作
1. VS2015等开发工具
2. 作为按钮背景的图片
三、代码
<Window x:Class="WPF图片按钮.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF图片按钮"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid> <!—网格布局,一种布局方式-->
<Button HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" Background="SkyBlue" BorderThickness="0"> <!—调整按钮的样式(按钮大小,位置,内边距,背景颜色,边框粗细等等)-->
<Button.Content>
<StackPanel Orientation="Vertical">
<!—给按钮设置背景图片,Source为图片路径,Images为在项目内的根文件夹,后面是图片名称-->
<Image Stretch="Fill" Source="/Images/png-0652.ico" Width="30" Height="30" />
<!—给按钮设置文字-->
<TextBlock Text="删除" HorizontalAlignment="Center" />
</StackPanel>
</Button.Content>
</Button>
</Grid>
</Window>
四、 效果图片
上张图给哥们看看
这就是上面代码设计出来的效果,本人设计能力不怎么好啊,献丑了,如果还要更高级的图片按钮的话最好是自定义(重写)一个模板!
五、 总结
不积小流,无以成江海;不积跬步,无以至千里!