首页 新闻 会员 周边 捐助

wpf slider 怎么将滑块弄小点

0
悬赏园豆:100 [已解决问题] 解决于 2011-07-12 23:45

wpf slider 怎么将滑块弄小点 或者可以自定义一张图片啥的 gif动态的图片更好

yesicoo的主页 yesicoo | 初学一级 | 园豆:92
提问于:2011-07-09 02:36
< >
分享
最佳答案
0

书上常用的方法是布局法,用grid布局,将控件放在grid格子里,格子的大小决定控件的大小,一般不会用<StackPanel>布局,任何控件放在里面,大小都会平均分配,即便是设置了某个控件的大小,稳定性还是很难控制的。每个控件都有with和height属性。

如果想自定义一张图片,可以把背景改一下,好像不太好看。也可以加个标签。<Slider ><ControlTemplate></ControlTemplate></Slider >,在里边可以更改控件的形状,背景,显示样式。《WPF编程》这本书里面介绍的很清楚。

收获园豆:100
贪心狸猫 | 小虾三级 |园豆:872 | 2011-07-09 14:43
slider控件是个整体的 小滑块在上面 用Grid套在外面的话 调整大小 会将整个控件弄小 而不会将上面的小滑块弄小 可能我没有理解透您的意思 方便的话可以发个例子吗 yesicoo@163.com
yesicoo | 园豆:92 (初学一级) | 2011-07-09 16:36
试下把控件缩小,不过线条也变了







图片在你邮箱里面
贪心狸猫 | 园豆:872 (小虾三级) | 2011-07-10 22:18
非常感谢 非常感谢
yesicoo | 园豆:92 (初学一级) | 2011-07-12 23:45
其他回答(1)
0
<Window x:Class="msdnWPFforums.Window1"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
Title
="Window1" Height="300" Width="300">

<Window.Resources>

<Style x:Key="SliderIncrement" TargetType="{x:Type RepeatButton}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RepeatButton}">
<Canvas HorizontalAlignment="Right">
<Canvas Margin="-10,0,0,0">
<!-- Background Ellipse -->
<Path Fill="Black" Data=" M 0,3.334961 C 0,1.783203 1.257812,0.525391 2.810547,0.525391 C 4.362305,0.525391 5.620117,1.783203 5.620117,3.334961 C 5.620117,4.886719 4.362305,6.145508 2.810547,6.145508 C 1.257812,6.145508 0,4.886719 0,3.334961 Z"/>

<!-- Plus Vertical Line -->
<Path Fill="White" Data="F1 M 4.678711,3.135742 C 4.678711,3.043945 4.604492,2.969238 4.512695,2.969238 L 0.945312,2.968750 C 0.853515,2.968750 0.779297,3.042969 0.779297,3.135254 L 0.77832,3.680664 C 0.77832,3.772461 0.852539,3.847656 0.945312,3.847656 L 4.511719,3.847656 C 4.603515,3.847656 4.678711,3.773438 4.678711,3.681641 L 4.678711,3.135742 Z"/>

<!-- Plus Horizontal Line -->
<Path Fill="White" Data="F1 M 2.472656,1.440430 C 2.379883,1.440430 2.305664,1.514648 2.305664,1.606934 L 2.308594,5.176758 C 2.308594,5.268555 2.383789,5.343750 2.475586,5.343750 L 3.019531,5.343750 C 3.112305,5.343750 3.186523,5.268555 3.186523,5.176758 L 3.183594,1.607422 C 3.183594,1.515137 3.108398,1.440430 3.016601,1.440430 L 2.472656,1.440430 Z"/>
</Canvas>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Style x:Key="ThumbBall" TargetType="{x:Type Thumb}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Path StrokeThickness="0.250000" Stroke="#FFA2A2A2" StrokeMiterLimit="1.000000" Data="F1 M 0,3.455078 C 0,1.615723 1.490234,0.125000 3.330078,0.125000 C 5.168945,0.125000 6.659179,1.615723 6.659179,3.455078 C 6.659179,5.292969 5.168945,6.784180 3.330078,6.784180 C 1.490234,6.784180 0,5.292969 0,3.455078 Z" x:Name="TrackBall" Opacity="0.75" Fill="Blue" />

<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="TrackBall" Property="Opacity" Value="1"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="TrackBall" Property="Opacity" Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>

</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


<Style x:Key="SliderDecrement" TargetType="{x:Type RepeatButton}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RepeatButton}">
<Canvas>

<Canvas Margin="5,0,0,0">
<!-- Background Ellipse -->
<Path Fill="Black" Data=" M 0,3.334961 C 0,1.783203 1.257812,0.525391 2.810547,0.525391 C 4.362305,0.525391 5.620117,1.783203 5.620117,3.334961 C 5.620117,4.886719 4.362305,6.145508 2.810547,6.145508 C 1.257812,6.145508 0,4.886719 0,3.334961 Z"/>

<!-- Minus Sign -->
<Path Fill="White" Data="F1 M 4.678711,3.135742 C 4.678711,3.043945 4.604492,2.969238 4.512695,2.969238 L 0.945312,2.968750 C 0.853515,2.968750 0.779297,3.042969 0.779297,3.135254 L 0.77832,3.680664 C 0.77832,3.772461 0.852539,3.847656 0.945312,3.847656 L 4.511719,3.847656 C 4.603515,3.847656 4.678711,3.773438 4.678711,3.681641 L 4.678711,3.135742 Z"/>
</Canvas>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Style x:Key="SliderStyle" TargetType="{x:Type Slider}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Slider}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="{TemplateBinding Slider.MinHeight}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<Path Margin="15,0,0,0" Grid.Row="1" Fill="Black" Data="F1 M 0,2.701660 L 0,4.179688 L 170.306641,4.179688 L 170.306641,2.701660 L 0,2.701660 Z"/>

<Track Grid.Row="1" Name="PART_Track" Visibility="Visible">
<Track.DecreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderDecrement}"
Command
="Slider.DecreaseLarge" />
</Track.DecreaseRepeatButton>
<Track.Thumb>
<Thumb Margin="15,0,15,0" Style="{StaticResource ThumbBall}" />
</Track.Thumb>
<Track.IncreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderIncrement}"
Command
="Slider.IncreaseLarge" />
</Track.IncreaseRepeatButton>
</Track>
</Grid>

</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

</Window.Resources>

<StackPanel>
<Slider Minimum="0" Maximum="100" Style="{StaticResource SliderStyle}"/>
</StackPanel>
</Window>

代码摘自:http://social.msdn.microsoft.com/forums/en-US/wpf/thread/fb653519-f4ed-4053-8e4c-bea68d7ec1e3/

 

或参照下这篇文章:使用模板自定义 WPF 控件

鹤冲天 | 园豆:2379 (老鸟四级) | 2011-07-09 11:25
这个代码我看了 对我这个新手来说感觉相当的复杂 昨晚研究到四点多没弄明白 才发这个贴的 求高手做个小例子 yesicoo@163.com
支持(0) 反对(0) yesicoo | 园豆:92 (初学一级) | 2011-07-09 16:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册