无废话WPF系列18:控件模版

WPF圆形按钮设计
本文介绍如何使用WPF中的ControlTemplate来设计一个圆形按钮,并通过样式设置背景渐变效果。

控件模版ControlTemplate就是设置控件的外观,比如我们常见到的按钮是下面这样的,但是我们如何改变成圆形的呢?

imageimage

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
< Window  x:Class="DeepXAML.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:DeepXAML"      
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         Title="MainWindow" Height="250" Width="450">
     < Window.Resources >
         < Style  x:Key="roundButton" TargetType="Button">
             < Setter  Property="Background">
                 < Setter.Value >
                     < LinearGradientBrush  StartPoint="0.5,0" EndPoint="0.5,1">
                         < LinearGradientBrush.GradientStops >
                             < GradientStop  Offset="0.0" Color="#fff" />
                             < GradientStop  Offset="1.0" Color="Red" />
                         </ LinearGradientBrush.GradientStops >
                     </ LinearGradientBrush >
                 </ Setter.Value >
             </ Setter >
             < Setter  Property="Template">
                 < Setter.Value >
                     < ControlTemplate  TargetType="Button">
                         < Grid >
                             < Ellipse  Fill="{TemplateBinding Background}"></ Ellipse >
                             < ContentPresenter  Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center" />                        </ Grid >                  
                     </ ControlTemplate >
                 </ Setter.Value >
             </ Setter >
         </ Style >   
     </ Window.Resources >
     < StackPanel  x:Name="stackPanel">
         < Button  Width="200" Height="120" Margin="20" Style="{StaticResource ResourceKey=roundButton}">OK</ Button >
     </ StackPanel >
</ Window >

ItemsControl有个PanelTemplate可以控制ItemsControl的条目容器。

本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2011/02/27/1966176.html如需转载请自行联系原作者


王德水

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值