[WPF] Slider Custom StyleFrontend/WPF2022. 9. 25. 21:38
Table of Contents
반응형
WPF 기본 Slider Stlye에서 Thumb을 드래그할 때마다 박스 안에 숫자가 표시되도록 Style을 하였습니다.
Slider 모양
Style 추가
App.xaml 에 추가하거나 Resource.xaml 파일을 생성하여 Style 을 추가합니다.
<SolidColorBrush x:Key="CustomSliderBackgroundSolidColorBrush" Color="#1E211B" />
<LinearGradientBrush x:Key="CustomSliderForegroundBrush" StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#65351F" Offset="0.2" />
<GradientStop Color="#9B5B2B" Offset="0.5" />
<GradientStop Color="#65351F" Offset="0.8" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="CustomSliderThumBrush" StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#3B3C39" Offset="0.2" />
<GradientStop Color="#454543" Offset="0.5" />
<GradientStop Color="#3B3C39" Offset="0.8" />
</LinearGradientBrush>
<Style x:Key="CustomSliderThumbStyle" TargetType="{x:Type Thumb}">
<Setter Property="Focusable" Value="false"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Height" Value="20"/>
<Setter Property="Width" Value="30"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Canvas SnapsToDevicePixels="true">
<Grid Height="20" Width="30">
<Rectangle x:Name="Background"
Fill="{StaticResource CustomSliderThumBrush}" Stroke="#FFDADADA"
Height="20" Width="30"
RadiusX="3" RadiusY="3"/>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
Foreground="White" FontSize="10"
Text="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type Slider}}}"/>
</Grid>
</Canvas>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Fill" TargetName="Background" Value="Orange"/>
</Trigger>
<Trigger Property="IsDragging" Value="true">
<Setter Property="Fill" TargetName="Background" Value="{StaticResource CustomSliderThumBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Fill" TargetName="Background" Value="Gray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="CustomSliderControlTemplate" TargetType="{x:Type Slider}">
<Border Background="Transparent" SnapsToDevicePixels="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="{TemplateBinding MinHeight}" />
</Grid.RowDefinitions>
<DockPanel LastChildFill="True">
<Border x:Name="PART_SelectionRange" Height="5" ClipToBounds="True" Visibility="Visible">
<Rectangle Margin="0 0 -10 0" RadiusX="2" RadiusY="2" Fill="{StaticResource CustomSliderForegroundBrush}" />
</Border>
<Border ClipToBounds="True" Height="5" Visibility="Visible">
<Rectangle Margin="0 0 0 0" RadiusX="2" RadiusY="2" Fill="{StaticResource CustomSliderBackgroundSolidColorBrush}" />
</Border>
</DockPanel>
<Track x:Name="PART_Track">
<Track.Thumb>
<Thumb Style="{StaticResource CustomSliderThumbStyle}" VerticalAlignment="Center"
Width="{TemplateBinding MinWidth}" Height="{TemplateBinding MinHeight}" OverridesDefaultStyle="True" Focusable="False" />
</Track.Thumb>
</Track>
</Grid>
</Border>
</ControlTemplate>
<Style x:Key="CustomSliderStyle" TargetType="{x:Type Slider}">
<Setter Property="Template" Value="{StaticResource CustomSliderControlTemplate}" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="MinWidth" Value="30" />
<Setter Property="MinHeight" Value="20" />
<Setter Property="Height" Value="20" />
<Setter Property="MaxHeight" Value="20" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="AutoToolTipPlacement" Value="None" />
<Setter Property="IsMoveToPointEnabled" Value="True" />
<Setter Property="SelectionStart" Value="0" />
<Setter Property="SelectionEnd" Value="{Binding Path=Value, RelativeSource={RelativeSource Self}}" />
<Setter Property="Stylus.IsPressAndHoldEnabled" Value="false" />
</Style>
Slider Style 적용
아래와 같이 Slider 에 Style을 지정합니다.
<Slider Minimum="1" Maximum="255" IsSnapToTickEnabled="True" Style="{StaticResource CustomSliderStyle}"/>
반응형
'Frontend > WPF' 카테고리의 다른 글
[WPF] 이미지 불러오기 (0) | 2022.10.03 |
---|---|
[Devexpress] GridControl 자동 번호 매기기 (0) | 2022.10.03 |
[WPF] Prism ViewModelBase 정의 (0) | 2022.09.25 |
[WPF] Event Notifier 사용 방법 (0) | 2022.09.15 |
[WPF] Prism Library 사용 방법 (0) | 2022.08.28 |
@고지니어스 :: 규니의 개발 블로그
IT 기술과 개발 내용을 포스팅하는 블로그
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!