怎么在WPF中修改Slider滑动条颜色-创新互联

怎么在WPF 中修改Slider滑动条颜色?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于成都定制网页设计,高端网页制作,对展览展示等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业seo优化排名优化,H5建站,响应式网站。
<Style x:Key="DecreaseBtn" TargetType="{x:Type RepeatButton}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type RepeatButton}">
            <Border Background="{TemplateBinding Background}" 
                Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
              <!--轨迹,设置Background-->
              <Border Margin="0,0,-1,0" Background="{StaticResource SliderThumb.Track.DecreaseBackground}" 
                  VerticalAlignment="center" Height="4.0" />
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

完整代码(只是考虑水平的Slider):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <SolidColorBrush x:Key="SliderThumb.Static.Foreground" Color="#FFE5E5E5"/>
  <SolidColorBrush x:Key="SliderThumb.MouseOver.Background" Color="Gray"/>
  <SolidColorBrush x:Key="SliderThumb.MouseOver.Border" Color="#FF7Eb4EA"/>
  <SolidColorBrush x:Key="SliderThumb.Pressed.Background" Color="Gray"/>
  <SolidColorBrush x:Key="SliderThumb.Pressed.Border" Color="Gray"/>
  <SolidColorBrush x:Key="SliderThumb.Disabled.Background" Color="#FFF0F0F0"/>
  <SolidColorBrush x:Key="SliderThumb.Disabled.Border" Color="#FFD9D9D9"/>
  <SolidColorBrush x:Key="SliderThumb.Static.Background" Color="#989898"/>
  <ControlTemplate x:Key="SliderThumbHorizontalTop" TargetType="{x:Type Thumb}">
    <Grid HorizontalAlignment="Center" UseLayoutRounding="True" VerticalAlignment="Center">
      <Path x:Name="grip" Data="M 0,6 C0,6 5.5,0 5.5,0 5.5,0 11,6 11,6 11,6 11,18 11,18 11,18 0,18 0,18 0,18 0,6 0,6 z" 
         Fill="{StaticResource SliderThumb.Static.Background}" 
         Stretch="Fill" SnapsToDevicePixels="True" 
         Stroke="{Binding Path=Fill, RelativeSource={x:Static RelativeSource.Self}}" StrokeThickness="1" UseLayoutRounding="True" VerticalAlignment="Center"/>
    </Grid>
    <ControlTemplate.Triggers>
      <Trigger Property="IsMouseOver" Value="true">
        <Setter Property="Fill" TargetName="grip" Value="{StaticResource SliderThumb.MouseOver.Background}"/>
        <Setter Property="Stroke" TargetName="grip" Value="{StaticResource SliderThumb.MouseOver.Border}"/>
      </Trigger>
      <Trigger Property="IsDragging" Value="true">
        <Setter Property="Fill" TargetName="grip" Value="{StaticResource SliderThumb.Pressed.Background}"/>
        <Setter Property="Stroke" TargetName="grip" Value="{StaticResource SliderThumb.Pressed.Border}"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Fill" TargetName="grip" Value="{StaticResource SliderThumb.Disabled.Background}"/>
        <Setter Property="Stroke" TargetName="grip" Value="{StaticResource SliderThumb.Disabled.Border}"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
  <ControlTemplate x:Key="SliderThumbHorizontalBottom" TargetType="{x:Type Thumb}">
    <Grid HorizontalAlignment="Center" UseLayoutRounding="True" VerticalAlignment="Center">
      <Path x:Name="grip" Data="M 0,12 C0,12 5.5,18 5.5,18 5.5,18 11,12 11,12 11,12 11,0 11,0 11,0 0,0 0,0 0,0 0,12 0,12 z" 
         Fill="{StaticResource SliderThumb.Static.Background}"
         Stretch="Fill" 
         SnapsToDevicePixels="True" 
         Stroke="{Binding Path=Fill, RelativeSource={x:Static RelativeSource.Self}}" StrokeThickness="1" UseLayoutRounding="True" VerticalAlignment="Center"/>
    </Grid>
    <ControlTemplate.Triggers>
      <Trigger Property="IsMouseOver" Value="true">
        <Setter Property="Fill" TargetName="grip" Value="{StaticResource SliderThumb.MouseOver.Background}"/>
        <Setter Property="Stroke" TargetName="grip" Value="{StaticResource SliderThumb.MouseOver.Border}"/>
      </Trigger>
      <Trigger Property="IsDragging" Value="true">
        <Setter Property="Fill" TargetName="grip" Value="{StaticResource SliderThumb.Pressed.Background}"/>
        <Setter Property="Stroke" TargetName="grip" Value="{StaticResource SliderThumb.Pressed.Border}"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Fill" TargetName="grip" Value="{StaticResource SliderThumb.Disabled.Background}"/>
        <Setter Property="Stroke" TargetName="grip" Value="{StaticResource SliderThumb.Disabled.Border}"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
  <SolidColorBrush x:Key="SliderThumb.Track.Background" Color="#b6b6b6"/>
  <SolidColorBrush x:Key="SliderThumb.Track.DecreaseBackground" Color="#45db5e"/>
  <Style x:Key="RepeatButtonTransparent" TargetType="{x:Type RepeatButton}">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Focusable" Value="false"/>
    <Setter Property="IsTabStop" Value="false"/>
  </Style>
  <Style x:Key="DecreaseBtn" TargetType="{x:Type RepeatButton}" BasedOn="{StaticResource RepeatButtonTransparent}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type RepeatButton}">
          <Border Background="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
            <Border Margin="1,0,-1,0" Background="{StaticResource SliderThumb.Track.DecreaseBackground}" 
                VerticalAlignment="center" Height="4.0" />
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  <Style x:Key="IncreaseBtn" TargetType="{x:Type RepeatButton}" BasedOn="{StaticResource RepeatButtonTransparent}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type RepeatButton}">
          <Border Background="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  <ControlTemplate x:Key="SliderThumbHorizontalDefault" TargetType="{x:Type Thumb}">
    <Grid HorizontalAlignment="Center" UseLayoutRounding="True" VerticalAlignment="Center">
      <Path x:Name="grip" Data="M0 512C0 229.230208 229.805588 0 512 0 794.769792 0 1024 229.805588 1024 512 1024 794.769792 794.194412 1024 512 1024 229.230208 1024 0 794.194412 0 512Z" 
           StrokeThickness="1" Fill="{StaticResource SliderThumb.Static.Background}" Stroke="{Binding Path=Fill, RelativeSource={x:Static RelativeSource.Self}}"
           Width="18" Height="{Binding Path=Width, RelativeSource={x:Static RelativeSource.Self}}"
           Stretch="Fill" SnapsToDevicePixels="True" UseLayoutRounding="True" VerticalAlignment="Center"/>
    </Grid>
    <ControlTemplate.Triggers>
      <Trigger Property="IsMouseOver" Value="true">
        <Setter Property="Fill" TargetName="grip" Value="{StaticResource SliderThumb.MouseOver.Background}"/>
      </Trigger>
      <Trigger Property="IsDragging" Value="true">
        <Setter Property="Fill" TargetName="grip" Value="{StaticResource SliderThumb.Pressed.Background}"/>
        <Setter Property="Stroke" TargetName="grip" Value="{StaticResource SliderThumb.Pressed.Border}"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Fill" TargetName="grip" Value="{StaticResource SliderThumb.Disabled.Background}"/>
        <Setter Property="Stroke" TargetName="grip" Value="{StaticResource SliderThumb.Disabled.Border}"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
  <ControlTemplate x:Key="SliderHorizontal" TargetType="{x:Type Slider}">
    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="15"/>
          <RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
          <RowDefinition Height="15"/>
        </Grid.RowDefinitions>
        <TickBar x:Name="TopTick" Fill="{TemplateBinding Foreground}" Height="4" Margin="0,0,0,2" Placement="Top" Grid.Row="0" 
               Visibility="Collapsed"/>
        <TickBar x:Name="BottomTick" Fill="{TemplateBinding Foreground}" Height="4" Margin="0,2,0,0" Placement="Bottom" Grid.Row="2" 
               Visibility="Collapsed"/>
        <Border x:Name="TrackBackground" BorderBrush="{StaticResource SliderThumb.Track.Background}" 
            BorderThickness="1" Background="{Binding Path=BorderBrush, RelativeSource={x:Static RelativeSource.Self}}" 
            Height="4.0" Margin="5,0" Grid.Row="1" VerticalAlignment="center">
          <Canvas HorizontalAlignment="Stretch" Margin="0,-1">
            <Rectangle x:Name="PART_SelectionRange" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"
                  Height="{Binding Path=Height, ElementName=TrackBackground}" Visibility="Hidden"/>
          </Canvas>
        </Border>
        <Track x:Name="PART_Track" Grid.Row="1">
          <Track.DecreaseRepeatButton>
            <RepeatButton Command="{x:Static Slider.DecreaseLarge}" Style="{StaticResource DecreaseBtn}"/>
          </Track.DecreaseRepeatButton>
          <Track.IncreaseRepeatButton>
            <RepeatButton Command="{x:Static Slider.IncreaseLarge}" Style="{StaticResource IncreaseBtn}"/>
          </Track.IncreaseRepeatButton>
          <Track.Thumb>
            <Thumb x:Name="Thumb" Focusable="False" Height="20" OverridesDefaultStyle="True" 
                Template="{StaticResource SliderThumbHorizontalDefault}" VerticalAlignment="Center" 
                Width="{Binding Path=Height, RelativeSource={x:Static RelativeSource.Self}}"/>
          </Track.Thumb>
        </Track>
      </Grid>
    </Border>
    <ControlTemplate.Triggers>
      <Trigger Property="TickPlacement" Value="TopLeft">
        <Setter Property="Visibility" TargetName="TopTick" Value="Visible"/>
        <Setter Property="Template" TargetName="Thumb" Value="{StaticResource SliderThumbHorizontalTop}"/>
        <Setter Property="Margin" TargetName="TrackBackground" Value="5,2,5,0"/>
      </Trigger>
      <Trigger Property="TickPlacement" Value="BottomRight">
        <Setter Property="Visibility" TargetName="BottomTick" Value="Visible"/>
        <Setter Property="Template" TargetName="Thumb" Value="{StaticResource SliderThumbHorizontalBottom}"/>
        <Setter Property="Margin" TargetName="TrackBackground" Value="5,0,5,2"/>
      </Trigger>
      <Trigger Property="TickPlacement" Value="Both">
        <Setter Property="Visibility" TargetName="TopTick" Value="Visible"/>
        <Setter Property="Visibility" TargetName="BottomTick" Value="Visible"/>
      </Trigger>
      <Trigger Property="IsSelectionRangeEnabled" Value="true">
        <Setter Property="Visibility" TargetName="PART_SelectionRange" Value="Visible"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
  <Style x:Key="SliderStyle" TargetType="{x:Type Slider}">
    <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Template" Value="{StaticResource SliderHorizontal}"/>
  </Style>
</ResourceDictionary>

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联成都网站设计公司行业资讯频道,感谢您对创新互联成都网站设计公司的支持。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站设计器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

名称栏目:怎么在WPF中修改Slider滑动条颜色-创新互联
浏览路径:https://www.cdcxhl.com/article22/dgjgcc.html

成都网站建设公司_创新互联,为您提供网站内链ChatGPT服务器托管用户体验网站设计关键词优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站