书上常用的方法是布局法,用grid布局,将控件放在grid格子里,格子的大小决定控件的大小,一般不会用<StackPanel>布局,任何控件放在里面,大小都会平均分配,即便是设置了某个控件的大小,稳定性还是很难控制的。每个控件都有with和height属性。
如果想自定义一张图片,可以把背景改一下,好像不太好看。也可以加个标签。<Slider ><ControlTemplate></ControlTemplate></Slider >,在里边可以更改控件的形状,背景,显示样式。《WPF编程》这本书里面介绍的很清楚。
<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 控件