SilverLight自定义ImageButton
SilverLight中XAML的写法和WPF一样,但是发现在自定义按钮上,没有WPF来的容易,下面说说我制作SilverLight中的ImageButton的一些思路。
在SilverLight中,我们可以利用Button的Content属性来承载多个元素。然后通过stackPanel来包容图片和文本:
<Button x:Name="AddUserButton" Width="150" Margin="32,140,0,0" Height="38" VerticalAlignment="Top" HorizontalAlignment="Left" d:LayoutOverrides="Width">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Image Source="Images/eqrth.png" Width="16" Height="16" />
<TextBlock Text="Add User" Margin="10,0,0,0" />
</StackPanel>
</Button.Content>
</Button>
<Button x:Name="EditUserButton" Width="150" Margin="32,82,0,0" Height="38" VerticalAlignment="Top" HorizontalAlignment="Left" d:LayoutOverrides="Width">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Image Source="Images/eqrth.png" Width="16" Height="16" />
<TextBlock Text="Edit User" Margin="10,0,0,0" />
</StackPanel>
</Button.Content>
</Button>
<Button x:Name="DeleteUserButton" Width="150" Margin="32,26,0,0" Height="38" VerticalAlignment="Top" HorizontalAlignment="Left" d:LayoutOverrides="Width">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Image Source="Images/eqrth.png" Width="16" Height="16" />
<TextBlock Text="Delete User" Margin="10,0,0,0" />
</StackPanel>
</Button.Content>
</Button>
这样,我们就能够得到想要的效果,图片可以通过重设Image的Source属性来变换。
但是如果我们项目中很多地方用到了这个配置文件的话,那么我们最好把它放到资源文件中,然后引用即可:
首先,创建一个ResourceDictionary页面。
然后,将样式写入进去:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- 应该在此定义资源字典条目。--> <Style x:Key="AddButtonStyle" TargetType="Button">
<Setter Property="Width" Value="150" />
<Setter Property="Margin" Value="0,0,0,10" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="Images/eqrth.png" Width="16" Height="16" />
<ContentPresenter Content="{Binding}" Margin="10,0,0,0" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style> <Style x:Key="DeleteButtonStyle" TargetType="Button">
<Setter Property="Width" Value="150" />
<Setter Property="Margin" Value="0,0,0,10" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="Images/eqrth.png" Width="16" Height="16" />
<ContentPresenter Content="{Binding}" Margin="10,0,0,0" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style> <Style x:Key="EditButtonStyle" TargetType="Button">
<Setter Property="Width" Value="150" />
<Setter Property="Margin" Value="0,0,0,10" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="Images/eqrth.png" Width="16" Height="16" />
<ContentPresenter Content="{Binding}" Margin="10,0,0,0" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style> </ResourceDictionary>
这里需要注意的是,每一个Style必须分配X:key属性,并且属性ID唯一。
最后,在前端页面,我们可以这样使用:
先引入样式资源:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionary1.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
然后Apply上样式即可:
<Button HorizontalAlignment="Left" Margin="32,0,0,160"
VerticalAlignment="Bottom"
Width="152"
Content="添加用户信息"
Style="{StaticResource AddButtonStyle}" Height="40" RenderTransformOrigin="-2,0.2"
/>
SilverLight自定义ImageButton的更多相关文章
- Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象
原文:Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象 原创文章,如需转载,请注明出处. 最近在一直研究Silve ...
- Silverlight 用DependencyProperty 自定义ImageButton控件 定义属性
为ImageButton自定义IconSource和Contents属性 xaml代码 <UserControl x:Class="SilverlightCreate.Silverli ...
- Silverlight 创建 ImageButton
这几天一直在折腾怎么在silverlight 按钮上添加图片,直接向imagebutton那样设置成属性可以直接更改,最后到处查找资料终于搞出一个imagebutton了. <Style x:K ...
- WPF 自定义 ImageButton
控件源码: public class ImageButton : Button { public ImageButton() { } public string No ...
- Flex4 自定义通用的ImageButton
Flex4与之前版本的一个极大区别就是外观皮肤的分离,虽然进一步解耦,但存在一个不爽的地方就是增加了编码的工作量,你能想象为你的每个自定义组件都写一个对应的皮肤吗?可能仅仅和你之前写过的组件差了那么一 ...
- android控件---自定义带文本的ImageButton
由于SDK提供的ImageButton只能添加图片,不能添加文字:而Button控件添加的文字只能显示在图片内部:当我们需要添加文字在图片外部时就不能满足我们的需求了,顾只能自己写个自定义ImageB ...
- Android 自定义Android带图片和文字的ImageButton
经过分析,上述按钮效果实际上就是一个布局,一个最简单不过的垂直线性布局,上部分是一个ImageView,下部分是一个TextView,这个布局可点击.可设置监听. 我们首先要编写自己的ImageBut ...
- 带文字的ImageButton[自定义]
今天Android项目中遇到一个ImageButton控件上面要显示文字,无奈自定义了一个ImageButton,继承自ImageButton.其实就是override这个控件的onDraw(Canv ...
- [UWP]附加属性2:实现一个Canvas
5. 附加属性实践:自定义Canvas 附加属性在UWP中是一个十分重要的组成部分,很多功能都依赖于附加属性实现,典型的例子是常用的Grid和Canvas.通常附加属性有三个使用场景:插入属性.触发行 ...
随机推荐
- 关于UI资源获取资源的好的网站
前言:和我一样喜欢UI的一定喜欢这里的内容. 下面是关于sketch资源获取网页,点击图片就能进入: 连接是:https://github.com/JakeLin 居然意外百度到Sketch中国,还提 ...
- 快捷下载 sourceForge下的资源
一些开源项目通常会放在 sourceforge.net下面发布.然而,这个网站有时候出现卡顿,并且需要点击几次页面才能下载到自己想要的资源. 这里有个好办法,一步列出所有可下载的资源: ...
- jquery miniui , 普加甘特图,流程管理
http://www.miniui.com/docs/quickstart/index.html 普加 甘特图 流程管理 http://www.plusgantt.com/project/demo/P ...
- 手动将自定制的WebPart部署到 SharePoint 2010 中
1.搭建好开发环境,建立webpart工程,写代码. 2.修改assembly.cs文件 在部署前,需要修改assembly文件,增加以下两句: using System.Security; [a ...
- 在SQL2008R2查询分析器出错(在执行批处理时出现错误。错误消息为: 目录名称无效。)
在用SQL2008R2查询分析器时 SELECT * FROM 表名; 出错: 在执行批处理时出现错误.错误消息为: 目录名称无效. 原因: 在打开查询分析器时,用360软件清空了临时文件(只是偶尔1 ...
- 10901 Missile
10901 Missile 时间限制:1000MS 内存限制:65535K提交次数:40 通过次数:7 Description Long, long ago, country A invented ...
- Linux上Eclipse项目右键菜单没有Maven
在Centos 7上安装了eclipse以后,着实很兴奋.eclipse luna版本自带maven.但是用mvn eclipse:eclipse创建的java工程,在右键菜单居然没有Maven按钮, ...
- codeforces 724
题目链接: http://codeforces.com/contest/724 A. Checking the Calendar time limit per test 1 second memory ...
- poj2387 Til the Cows Come Home 最短路径dijkstra算法
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- ckplayer播放器去掉右边的开关灯分享插件
在上一篇文章中介绍了如何使用ckplayer播放器,但是有的人不需要CK播放器右边的开关灯分享插件,那么就需要把该插件给去掉,方法也很简单. 第一步:先打开ckplayer.js里找到下面三行删除掉 ...