下面看一个Tick控件的例子,这只是演示,Tick单独使用没有意义。

  1. <TickBar Height="15" Width="180" Ticks="10,35,50,70" Maximum="100"
  2. Minimum="0" Fill="DarkMagenta" Placement="Top" />

好了,现在我们可以自定义一个Slider,这个例子是水平的,它用一个Grid来布局,共三行,最上和最下行分别放一个TickBar用于显示刻度,中间放一个Track为主体部分。

为了能动态显示刻度值,我们把Slider的Value属性绑定到TextBlock的Text属性,这样,只要Slider控件的值发生改变,TextBlock中就能动态显示,前面我们说过了,WPF的属性系统都是依赖项属性,因此可以动态关联。

  1. <Window x:Class="Sample_TickBar.Win2"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Win2" Height="300" Width="550">
  5. <Window.Resources>
  6. <Style x:Key="StyleForRepeatButton" TargetType="{x:Type RepeatButton}">
  7. <Style.Setters>
  8. <Setter Property="Background">
  9. <Setter.Value>
  10. <LinearGradientBrush
  11. StartPoint="0.5,0"
  12. EndPoint="0.5,1">
  13. <GradientStop Color="LightGreen" Offset="0"/>
  14. <GradientStop Color="Yellow" Offset="1"/>
  15. </LinearGradientBrush>
  16. </Setter.Value>
  17. </Setter>
  18. <Setter Property="Height" Value="10"/>
  19. <Setter Property="BorderBrush" Value="Transparent"/>
  20. <Setter Property="Focusable" Value="False"/>
  21. </Style.Setters>
  22. <Style.Triggers>
  23. <Trigger Property="IsPressed" Value="True">
  24. <Setter Property="Background">
  25. <Setter.Value>
  26. <LinearGradientBrush StartPoint="0.5,0"
  27. EndPoint="0.5,1">
  28. <GradientStop Color="LightBlue" Offset="0"/>
  29. <GradientStop Color="SkyBlue" Offset="1"/>
  30. </LinearGradientBrush>
  31. </Setter.Value>
  32. </Setter>
  33. </Trigger>
  34. </Style.Triggers>
  35. </Style>
  36. <ControlTemplate x:Key="tmpThumb" TargetType="{x:Type Thumb}">
  37. <Ellipse Name="e" Width="13" MinHeight="20" Fill="Blue"/>
  38. <ControlTemplate.Triggers>
  39. <Trigger Property="IsMouseOver" Value="True">
  40. <Setter TargetName="e" Property="Fill" Value="Red"/>
  41. </Trigger>
  42. </ControlTemplate.Triggers>
  43. </ControlTemplate>
  44. <ControlTemplate x:Key="tmp" TargetType="{x:Type Slider}">
  45. <Grid>
  46. <Grid.RowDefinitions>
  47. <RowDefinition Height="auto"/>
  48. <RowDefinition Height="auto" MinHeight="25"/>
  49. <RowDefinition Height="auto"/>
  50. </Grid.RowDefinitions>
  51. <TickBar x:Name="top" Fill="Magenta" Grid.Row="0" HorizontalAlignment="Stretch"
  52. Placement="Top" Height="8"
  53. Visibility="Collapsed"/>
  54. <Track x:Name="PART_Track" Grid.Row="1" HorizontalAlignment="Stretch">
  55. <Track.IncreaseRepeatButton>
  56. <RepeatButton Style="{StaticResource StyleForRepeatButton}"
  57. Command="Slider.IncreaseLarge"/>
  58. </Track.IncreaseRepeatButton>
  59. <Track.DecreaseRepeatButton>
  60. <RepeatButton Style="{StaticResource StyleForRepeatButton}"
  61. Command="Slider.DecreaseLarge"/>
  62. </Track.DecreaseRepeatButton>
  63. <Track.Thumb>
  64. <Thumb Height="20" Template="{StaticResource tmpThumb}"/>
  65. </Track.Thumb>
  66. </Track>
  67. <TickBar x:Name="Bottom" Grid.Row="2" Fill="Magenta" HorizontalAlignment="Stretch"
  68. Visibility="Collapsed" Placement="Bottom" Height="8"/>
  69. </Grid>
  70. <ControlTemplate.Triggers>
  71. <Trigger Property="TickPlacement" Value="TopLeft">
  72. <Setter TargetName="top" Property="Visibility" Value="Visible"/>
  73. </Trigger>
  74. <Trigger Property="TickPlacement" Value="BottomRight">
  75. <Setter Property="Visibility" TargetName="Bottom" Value="Visible"/>
  76. </Trigger>
  77. <Trigger Property="TickPlacement" Value="Both">
  78. <Setter TargetName="top" Property="Visibility" Value="Visible"/>
  79. <Setter TargetName="Bottom" Property="Visibility" Value="Visible"/>
  80. </Trigger>
  81. </ControlTemplate.Triggers>
  82. </ControlTemplate>
  83. </Window.Resources>
  84. <Grid>
  85. <Grid.RowDefinitions>
  86. <RowDefinition Height="50"/>
  87. <RowDefinition Height="auto"/>
  88. </Grid.RowDefinitions>
  89. <Slider x:Name="SliderTest" Grid.Row="0"  Margin="10,5,10,5" Maximum="100" Minimum="0" TickFrequency="1"
  90. Template="{StaticResource tmp}"
  91. Value="20" TickPlacement="BottomRight"/>
  92. <TextBlock Grid.Row="1" Text="{Binding Path=Value,ElementName=SliderTest}"
  93. FontFamily="宋体" FontSize="24" FontWeight="Bold"
  94. Margin="150,0,150,0" HorizontalAlignment="Center"/>
  95. </Grid>
  96. </Window>

继续聊WPF的更多相关文章

  1. 继续聊WPF——获取ComboBox中绑定的值

    千万不要认为WPF中的数据绑定会很复杂,尽管它的确比Winform程序灵活多了,但其本质是不变的,特别是ComboBox控件,我们知道在Winform中对该控件的有两个专为数据绑定而设定的属性——Di ...

  2. 继续聊WPF——如何获取ListView中选中的项

    在WPF中获Listview中选中的项,与WinForm里面有着很大的区别,要亲身去研究一下在WPF中如果处理,其实也不难,来,下面我们一起来通过一个简单的示例来感悟一下吧. 第一步就是建立一个WPF ...

  3. 继续聊WPF——动态数据模板

    我为啥称之为“动态数据模板”?先看看下面的截图,今天,我们就是要实现这种功能. 大概是这样的,我们定义的DataTemplate是通过触发器动态应用到 ComboBoxItem 上. 这个下拉列表控件 ...

  4. 继续聊WPF——设置网格控件列标题的样式

    我很奇怪的是,微软那厮是怎么搞的,Blend里面居然不能编辑GridView的样式,十万般无奈之下,只好手写XAML来处理了. 要想知道一个控件的样式是如何设置,看控件类的定义很重要,我们来看看Gri ...

  5. 继续聊WPF——Thumb控件

    这个控件,真不好介绍,MSDN上也是草草几句,反正就是可以让用户拖动的玩意儿,但是,你会发现,当你在该控件上拖动时,它没有反响,也就是说这个东西默认不做任何操作的,它是赖在那里什么都不干,除非你去踢上 ...

  6. 继续聊WPF——自定义CheckBox控件外观

    上一篇文章中谈到了BulletDecorator控件,就是为自定义CheckBox控件的模板做准备,因为CheckBox需要比较严格的布局,正好,BulletDecorator控件就合适了,该控件的布 ...

  7. 继续聊WPF——进度条

    ProgressBar控件与传统WinForm使用方法完全一样,我们只需关注: Minimum——最小值,默认为0: Maximum——最大值,默认为100. Value——当前值.   关键是它的控 ...

  8. 继续聊WPF——Expander控件(1)

    这个控件最实用的地方,就是做导航栏. <StackPanel Margin="20,20" Width="100" Height="460&qu ...

  9. 继续聊WPF——为ListView的行设置样式

    <Window x:Class="Wpf_GridHeaderStyle_sample.Window1" xmlns="http://schemas.microso ...

随机推荐

  1. iOS7系统iLEX RAT冬青鼠安装教程:无需刷机还原纯净越狱系统

    全网科技 温馨提醒:iLEX RAT和Semi-Restore的作用都是让你的已越狱的设备恢复至越狱的初始状态. 可是要注意无论你是用iLexRAT冬青鼠还是Semi-restore.对于还原来说都存 ...

  2. HDU2037 事件排序问题

    题目要求: Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!&q ...

  3. Android WiFi开发教程(三)——WiFi热点数据传输

    在上一篇文章中介绍了WiFi的搜索和连接,如果你还没阅读过,建议先阅读上一篇Android WiFi开发教程(二)——WiFi的搜索和连接.本篇接着简单介绍手机上如何通过WiFi热点进行数据传输. 跟 ...

  4. NYOJ15括号匹配

    NYOJ15括号匹配 括号匹配(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 给你一个字符串,里面只包含"(",")" ...

  5. linux随便贴贴

    在bin目录下进入mysql: ./mysql -uroot -p123456 update mysql.user set password=password('root') where user=' ...

  6. Python 42 mysql用户管理 、pymysql模块

    一:mysql用户管理 什么是mysql用户管理 mysql是一个tcp服务器,应用于操作服务器上的文件数据,接收用户端发送的指令,接收指令时需要考虑到安全问题, ATM购物车中的用户认证和mysql ...

  7. BZOJ 4010 拓扑排序+heap

    思路: 反向图求最大拓扑序 反向输出 //By SiriusRen #include <queue> #include <cstdio> #include <cstrin ...

  8. 2015 多校赛 第四场 1010 (hdu 5336)

    Problem Description XYZ is playing an interesting game called "drops". It is played on a r ...

  9. HTML 简要概述

    注: 说实在的,这门语言的确不需要太多或太详细的篇幅来大书特书.掌握个大概,知道些特点及特性也就差不多了.人脑不是电脑,不需要死记硬背许多的属性和值,有一本帮助手册在手,胜过千言万语. 什么是 HTM ...

  10. 7.union

    联合结果集union 简单的结果集联合: select number,name,age from emp union select cardnumber,name,age from emp2 基本的原 ...