继续聊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 ...
随机推荐
- Python内置的字符串处理函数
生成字符串变量 str='python String function' 字符串长度获取:len(str) 例:print '%s length=%d' % (str,len(str)) 连接字符 ...
- bootstrap简单form表单样式-form-horizontal
jsp代码: <div id="content" style="background-color: white;"> <form class= ...
- android按钮被点击文字颜色变化效果
有的时候做应用需要点击按钮时文字颜色也跟着变,松开后又还原,目前发现两种解决方案:第一用图片,如果出现的地方比较多,那么图片的量就相当可观:第二,也就是本文讲到的.废话少说,先贴图片,再上代码. 正常 ...
- Android按键添加和处理的方案【转】
本文转载自:http://www.cnblogs.com/skywang12345/p/3142851.html Android按键添加和处理的方案 版本号 说明 作者 日期 1.0 Andro ...
- Codeforces--615B--Longtail Hedgehog(贪心模拟)
B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- Codeforces--630B--Moore's Law(快速幂)
Moore's Law Time Limit: 500MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit ...
- EOJ 1641/UVa The SetStack Computer
Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...
- MySQL SQL优化教程
转自:https://www.cnblogs.com/duanxz/archive/2013/02/01/2889413.html 一,查询SQL执行效率 通过show status命令了解各种SQL ...
- 【IDEA】(4)---很好用的DEBUG功能
IDEA-DEBUG功能 一.常用快捷键 快捷键并不是完全一样的,我这边是MAC安装的IDEA, 这边最主要还是知道DEBUG时常用的功能. 1.快捷键 F7 #进入下一步,如果当前行是一个方法,则进 ...
- Node.js文件系统Api总结
//公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile(filename,[options ...