首页 > 代码库 > WPF初学之绘制自己需要的Slider(之二)

WPF初学之绘制自己需要的Slider(之二)

结合之前的样式,做了一下修改和美化,贴出来!!!

  1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  2                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  3   4     <!--Control colors.-->  5     <Color x:Key="DisabledControlDarkColor">#FFC5CBF9</Color>  6     <Color x:Key="ControlLightColor">#4D4D4D</Color>  7     <Color x:Key="ControlMediumColor">#FF7381F9</Color>  8     <Color x:Key="ControlDarkColor">#4D4D4D</Color>  9     <Color x:Key="ControlMouseOverColor">#FF3843C4</Color> 10     <Color x:Key="ControlPressedColor">#FF211AA9</Color> 11     <Color x:Key="GlyphColor">#FF444444</Color> 12     <Color x:Key="BorderLightColor">#FFCCCCCC</Color> 13     <Color x:Key="BorderDarkColor">#FF444444</Color> 14     <Color x:Key="DefaultBorderBrushDarkColor">Black</Color> 15     <Color x:Key="SliderTrackDarkColor">#4D4D4D</Color> 16  17     <!--StyleLeft--> 18     <Style x:Key="StyleLeft" TargetType="{x:Type RepeatButton}"> 19         <Style.Setters> 20             <Setter Property="Background" > 21                 <Setter.Value> 22                     <LinearGradientBrush  StartPoint="0.5,0"   EndPoint="0.5,1"> 23                         <!--<GradientStop Color="LightBlue" Offset="0"/>--> 24                         <GradientStop Color="Red" Offset="1"/> 25                     </LinearGradientBrush> 26                 </Setter.Value> 27             </Setter> 28             <Setter Property="Height" Value=http://www.mamicode.com/"5"/> 29             <!--<Setter Property="BorderBrush" Value=http://www.mamicode.com/"#4D4D4D"/> 30             <Setter Property="BorderThickness" Value=http://www.mamicode.com/"0"></Setter> 31             <Setter Property="Focusable" Value=http://www.mamicode.com/"False"/>--> 32             <Setter Property="Control.Template"> 33                 <Setter.Value> 34                     <ControlTemplate TargetType="{x:Type RepeatButton}"> 35                         <Border x:Name="Chrome"  36                                 BorderBrush="{TemplateBinding BorderBrush}"  37                                 Background="{TemplateBinding Background}" 38                                 SnapsToDevicePixels="true"> 39                             <ContentPresenter  40                                 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"  41                                 Margin="{TemplateBinding Padding}"  42                                 RecognizesAccessKey="True"  43                                 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  44                                 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 45                         </Border> 46                     </ControlTemplate> 47                 </Setter.Value> 48             </Setter> 49         </Style.Setters> 50         <Style.Triggers> 51             <Trigger Property="IsPressed" Value=http://www.mamicode.com/"True"> 52                 <Setter Property="Background"> 53                     <Setter.Value> 54                         <LinearGradientBrush StartPoint="0.5,0"  EndPoint="0.5,1"> 55                             <GradientStop Color="LightBlue" Offset="0"/> 56                             <GradientStop Color="SkyBlue" Offset="1"/> 57                         </LinearGradientBrush> 58                     </Setter.Value> 59                 </Setter> 60             </Trigger> 61         </Style.Triggers> 62     </Style> 63  64     <!--<StyleRight>--> 65     <Style x:Key="StyleRight" 66          TargetType="{x:Type RepeatButton}"> 67         <Setter Property="SnapsToDevicePixels" 68             Value=http://www.mamicode.com/"true" /> 69         <Setter Property="OverridesDefaultStyle" 70             Value=http://www.mamicode.com/"true" /> 71         <Setter Property="IsTabStop" 72             Value=http://www.mamicode.com/"false" /> 73         <Setter Property="Focusable" 74             Value=http://www.mamicode.com/"false" /> 75         <Setter Property="Template"> 76             <Setter.Value> 77                 <ControlTemplate TargetType="{x:Type RepeatButton}"> 78                     <Border Background="Transparent" /> 79                 </ControlTemplate> 80             </Setter.Value> 81         </Setter> 82     </Style> 83  84     <!--ThumbStyleByImage--> 85     <Style x:Key="ThumbStyleByImage" 86          TargetType="{x:Type Thumb}"> 87         <Setter Property="SnapsToDevicePixels" 88             Value=http://www.mamicode.com/"true" /> 89         <Setter Property="OverridesDefaultStyle" 90             Value=http://www.mamicode.com/"true" /> 91         <Setter Property="Height" 92             Value=http://www.mamicode.com/"23" /> 93         <Setter Property="Width" 94             Value=http://www.mamicode.com/"23" /> 95         <Setter Property="Template"> 96             <Setter.Value> 97                 <ControlTemplate TargetType="{x:Type Thumb}"> 98                     <Ellipse x:Name="Ellipse" 99                    StrokeThickness="1">100                         <Ellipse.Fill>101                             <SolidColorBrush Color="Green" ></SolidColorBrush>102                         </Ellipse.Fill>103                     </Ellipse>104                 </ControlTemplate>105             </Setter.Value>106         </Setter>107     </Style>108 109     <!--HSlider-->110     <ControlTemplate x:Key="HSlider"111                    TargetType="{x:Type Slider}">112         <Grid>113             <Grid.RowDefinitions>114                 <RowDefinition Height="Auto" />115                 <RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}" />116                 <RowDefinition Height="Auto" />117             </Grid.RowDefinitions>118             <TickBar x:Name="TopTick"119                SnapsToDevicePixels="True"120                Placement="Top"121                Height="4"122                Visibility="Collapsed">123                 <TickBar.Fill>124                     <SolidColorBrush Color="{DynamicResource GlyphColor}" />125                 </TickBar.Fill>126             </TickBar>127             <Border x:Name="TrackBackground"128               Margin="0"129               CornerRadius="0"130               Height="5"131               Grid.Row="1"132 133               BorderThickness="0.5">134                 <Border.BorderBrush>135                     <LinearGradientBrush StartPoint="0,0"136                                EndPoint="0,1">137                         <LinearGradientBrush.GradientStops>138                             <GradientStopCollection>139                                 <GradientStop Color="{DynamicResource BorderLightColor}"140                               Offset="0.0" />141                                 <GradientStop Color="{DynamicResource BorderDarkColor}"142                               Offset="1.0" />143                             </GradientStopCollection>144                         </LinearGradientBrush.GradientStops>145                     </LinearGradientBrush>146                 </Border.BorderBrush>147                 <Border.Background>148                     <LinearGradientBrush StartPoint="0,0"149                                EndPoint="0,1">150                         <LinearGradientBrush.GradientStops>151                             <GradientStopCollection>152                                 <GradientStop Color="{DynamicResource ControlLightColor}"153                               Offset="0.0" />154                                 <GradientStop Color="{DynamicResource SliderTrackDarkColor}"155                               Offset="1.0" />156                             </GradientStopCollection>157                         </LinearGradientBrush.GradientStops>158                     </LinearGradientBrush>159                 </Border.Background>160             </Border>161             <Track Grid.Row="1"162              x:Name="PART_Track">163                 <Track.DecreaseRepeatButton>164                     <RepeatButton Style="{StaticResource StyleLeft}"165                         Command="Slider.DecreaseLarge" />166                 </Track.DecreaseRepeatButton>167                 <Track.Thumb>168                     <Thumb Style="{StaticResource ThumbStyleByImage}" />169                 </Track.Thumb>170                 <Track.IncreaseRepeatButton>171                     <RepeatButton Style="{StaticResource StyleRight}"172                         Command="Slider.IncreaseLarge" />173                 </Track.IncreaseRepeatButton>174             </Track>175             <TickBar x:Name="BottomTick"176                SnapsToDevicePixels="True"177                Grid.Row="2"178                Fill="{TemplateBinding Foreground}"179                Placement="Bottom"180                Height="4"181                Visibility="Collapsed" />182         </Grid>183         <ControlTemplate.Triggers>184             <Trigger Property="TickPlacement"185                Value=http://www.mamicode.com/"TopLeft">186                 <Setter TargetName="TopTick"187                 Property="Visibility"188                 Value=http://www.mamicode.com/"Visible" />189             </Trigger>190             <Trigger Property="TickPlacement"191                Value=http://www.mamicode.com/"BottomRight">192                 <Setter TargetName="BottomTick"193                 Property="Visibility"194                 Value=http://www.mamicode.com/"Visible" />195             </Trigger>196             <Trigger Property="TickPlacement"197                Value=http://www.mamicode.com/"Both">198                 <Setter TargetName="TopTick"199                 Property="Visibility"200                 Value=http://www.mamicode.com/"Visible" />201                 <Setter TargetName="BottomTick"202                 Property="Visibility"203                 Value=http://www.mamicode.com/"Visible" />204             </Trigger>205         </ControlTemplate.Triggers>206     </ControlTemplate>207 208     <!--MainSlider-->209     <Style  x:Key="MainSlider" TargetType="{x:Type Slider}">210         <Setter Property="SnapsToDevicePixels"211             Value=http://www.mamicode.com/"true" />212         <Setter Property="OverridesDefaultStyle"213             Value=http://www.mamicode.com/"true" />214         <Style.Triggers>215             <Trigger Property="Orientation"216                Value=http://www.mamicode.com/"Horizontal">217                 <Setter Property="MinWidth"218                 Value=http://www.mamicode.com/"104" />219                 <Setter Property="MinHeight"220                 Value=http://www.mamicode.com/"21" />221                 <Setter Property="Template"222                 Value=http://www.mamicode.com/"{StaticResource HSlider}" />223             </Trigger>224         </Style.Triggers>225     </Style>226 227 </ResourceDictionary>

WPF初学之绘制自己需要的Slider(之二)