继续聊WPF
下面看一个Tick控件的例子,这只是演示,Tick单独使用没有意义。
- <TickBar Height="15" Width="180" Ticks="10,35,50,70" Maximum="100"
- Minimum="0" Fill="DarkMagenta" Placement="Top" />

好了,现在我们可以自定义一个Slider,这个例子是水平的,它用一个Grid来布局,共三行,最上和最下行分别放一个TickBar用于显示刻度,中间放一个Track为主体部分。
为了能动态显示刻度值,我们把Slider的Value属性绑定到TextBlock的Text属性,这样,只要Slider控件的值发生改变,TextBlock中就能动态显示,前面我们说过了,WPF的属性系统都是依赖项属性,因此可以动态关联。



- <Window x:Class="Sample_TickBar.Win2"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="Win2" Height="300" Width="550">
- <Window.Resources>
- <Style x:Key="StyleForRepeatButton" TargetType="{x:Type RepeatButton}">
- <Style.Setters>
- <Setter Property="Background">
- <Setter.Value>
- <LinearGradientBrush
- StartPoint="0.5,0"
- EndPoint="0.5,1">
- <GradientStop Color="LightGreen" Offset="0"/>
- <GradientStop Color="Yellow" Offset="1"/>
- </LinearGradientBrush>
- </Setter.Value>
- </Setter>
- <Setter Property="Height" Value="10"/>
- <Setter Property="BorderBrush" Value="Transparent"/>
- <Setter Property="Focusable" Value="False"/>
- </Style.Setters>
- <Style.Triggers>
- <Trigger Property="IsPressed" Value="True">
- <Setter Property="Background">
- <Setter.Value>
- <LinearGradientBrush StartPoint="0.5,0"
- EndPoint="0.5,1">
- <GradientStop Color="LightBlue" Offset="0"/>
- <GradientStop Color="SkyBlue" Offset="1"/>
- </LinearGradientBrush>
- </Setter.Value>
- </Setter>
- </Trigger>
- </Style.Triggers>
- </Style>
- <ControlTemplate x:Key="tmpThumb" TargetType="{x:Type Thumb}">
- <Ellipse Name="e" Width="13" MinHeight="20" Fill="Blue"/>
- <ControlTemplate.Triggers>
- <Trigger Property="IsMouseOver" Value="True">
- <Setter TargetName="e" Property="Fill" Value="Red"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- <ControlTemplate x:Key="tmp" TargetType="{x:Type Slider}">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="auto"/>
- <RowDefinition Height="auto" MinHeight="25"/>
- <RowDefinition Height="auto"/>
- </Grid.RowDefinitions>
- <TickBar x:Name="top" Fill="Magenta" Grid.Row="0" HorizontalAlignment="Stretch"
- Placement="Top" Height="8"
- Visibility="Collapsed"/>
- <Track x:Name="PART_Track" Grid.Row="1" HorizontalAlignment="Stretch">
- <Track.IncreaseRepeatButton>
- <RepeatButton Style="{StaticResource StyleForRepeatButton}"
- Command="Slider.IncreaseLarge"/>
- </Track.IncreaseRepeatButton>
- <Track.DecreaseRepeatButton>
- <RepeatButton Style="{StaticResource StyleForRepeatButton}"
- Command="Slider.DecreaseLarge"/>
- </Track.DecreaseRepeatButton>
- <Track.Thumb>
- <Thumb Height="20" Template="{StaticResource tmpThumb}"/>
- </Track.Thumb>
- </Track>
- <TickBar x:Name="Bottom" Grid.Row="2" Fill="Magenta" HorizontalAlignment="Stretch"
- Visibility="Collapsed" Placement="Bottom" Height="8"/>
- </Grid>
- <ControlTemplate.Triggers>
- <Trigger Property="TickPlacement" Value="TopLeft">
- <Setter TargetName="top" Property="Visibility" Value="Visible"/>
- </Trigger>
- <Trigger Property="TickPlacement" Value="BottomRight">
- <Setter Property="Visibility" TargetName="Bottom" Value="Visible"/>
- </Trigger>
- <Trigger Property="TickPlacement" Value="Both">
- <Setter TargetName="top" Property="Visibility" Value="Visible"/>
- <Setter TargetName="Bottom" Property="Visibility" Value="Visible"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Window.Resources>
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="50"/>
- <RowDefinition Height="auto"/>
- </Grid.RowDefinitions>
- <Slider x:Name="SliderTest" Grid.Row="0" Margin="10,5,10,5" Maximum="100" Minimum="0" TickFrequency="1"
- Template="{StaticResource tmp}"
- Value="20" TickPlacement="BottomRight"/>
- <TextBlock Grid.Row="1" Text="{Binding Path=Value,ElementName=SliderTest}"
- FontFamily="宋体" FontSize="24" FontWeight="Bold"
- Margin="150,0,150,0" HorizontalAlignment="Center"/>
- </Grid>
- </Window>
继续聊WPF的更多相关文章
- 继续聊WPF——获取ComboBox中绑定的值
千万不要认为WPF中的数据绑定会很复杂,尽管它的确比Winform程序灵活多了,但其本质是不变的,特别是ComboBox控件,我们知道在Winform中对该控件的有两个专为数据绑定而设定的属性——Di ...
- 继续聊WPF——如何获取ListView中选中的项
在WPF中获Listview中选中的项,与WinForm里面有着很大的区别,要亲身去研究一下在WPF中如果处理,其实也不难,来,下面我们一起来通过一个简单的示例来感悟一下吧. 第一步就是建立一个WPF ...
- 继续聊WPF——动态数据模板
我为啥称之为“动态数据模板”?先看看下面的截图,今天,我们就是要实现这种功能. 大概是这样的,我们定义的DataTemplate是通过触发器动态应用到 ComboBoxItem 上. 这个下拉列表控件 ...
- 继续聊WPF——设置网格控件列标题的样式
我很奇怪的是,微软那厮是怎么搞的,Blend里面居然不能编辑GridView的样式,十万般无奈之下,只好手写XAML来处理了. 要想知道一个控件的样式是如何设置,看控件类的定义很重要,我们来看看Gri ...
- 继续聊WPF——Thumb控件
这个控件,真不好介绍,MSDN上也是草草几句,反正就是可以让用户拖动的玩意儿,但是,你会发现,当你在该控件上拖动时,它没有反响,也就是说这个东西默认不做任何操作的,它是赖在那里什么都不干,除非你去踢上 ...
- 继续聊WPF——自定义CheckBox控件外观
上一篇文章中谈到了BulletDecorator控件,就是为自定义CheckBox控件的模板做准备,因为CheckBox需要比较严格的布局,正好,BulletDecorator控件就合适了,该控件的布 ...
- 继续聊WPF——进度条
ProgressBar控件与传统WinForm使用方法完全一样,我们只需关注: Minimum——最小值,默认为0: Maximum——最大值,默认为100. Value——当前值. 关键是它的控 ...
- 继续聊WPF——Expander控件(1)
这个控件最实用的地方,就是做导航栏. <StackPanel Margin="20,20" Width="100" Height="460&qu ...
- 继续聊WPF——为ListView的行设置样式
<Window x:Class="Wpf_GridHeaderStyle_sample.Window1" xmlns="http://schemas.microso ...
随机推荐
- Swift基本常识点
import Foundation // 单行注释 // 多行注释(支持嵌套,OC是不支持的) // 常量let,初始化之后就不可改变. // 常量的具体类型可以自动识别,等号后面是什么类型,它就是什 ...
- vbs use
VBScript中SendKeys的妙用 标签: vbscriptbasicmicrosoftinsertdeletestring 2011-05-26 15:29 1830人阅读 评论(0) 收藏 ...
- iOS 9 平台上 AFNetworking 框架 3.0 版本号解决的问题和问题解决
iOS 9 平台上 AFNetworking 框架 3.0 版本号解决的问题和问题解决 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名- ...
- bzoj4397【Usaco2015 Dec】Breed Counting
4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec Memory Limit: 128 MB Submit: 29 Solved: 25 ...
- SpringMVC + MyBatis 配置文件
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...
- 5200 fqy的难题----2的疯狂幂
5200 fqy的难题----2的疯狂幂 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description ...
- Android单选中listview中的一项
public class LipsListAdapter extends BaseAdapter { private Context context; private List<Lips> ...
- 微信公众号开发之文本消息自动回复,以及系统关注自动回复,php代码
以tshop为例 直接上代码: 企业 cc_wx_sys表为自建,存储系统消息的配置的 字段: id type key status <?php /** * tpshop * ========= ...
- 【DP悬线法】奶牛浴场
虽然还是悬线法,但是这道题可不能轻易地套模板了,而是要换一种思路,横着扫一遍,竖着扫一遍,时间复杂度依旧是O(n^2),然而空间复杂度有一定的优化 如果用原来的方法,显然时间空间都会炸(如果你想用ma ...
- EditPlus 1:更改默认编码方式
打开软件点击上面的菜单栏Tools(工具),再找到Configure User Tools(用户配置工具)点击,再找到左边栏File点击,这个时候可以看到右边栏的Default encoding点击可 ...