■ Control 클래스를 사용해 토글 스위치를 만드는 방법을 보여준다.
▶ THEMES/Generic.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject"> <Style TargetType="{x:Type local:ToggleSwitch}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:ToggleSwitch}"> <Grid> <Border Name="PART_Switch" Width="60" Height="30" CornerRadius="15" Background="{TemplateBinding Background}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Ellipse Name="PART_Thumb" Margin="2" Width="20" Height="20" Fill="White"> <Ellipse.RenderTransform> <TranslateTransform X="0" /> </Ellipse.RenderTransform> </Ellipse> <ContentPresenter Name="PART_ContentPresenter" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="PART_Switch" Property="Background" Value="{Binding OnBrush, RelativeSource={RelativeSource TemplatedParent}}" /> <Setter TargetName="PART_Thumb" Property="RenderTransform"> <Setter.Value> <TranslateTransform X="30" /> </Setter.Value> </Setter> <Setter TargetName="PART_ContentPresenter" Property="Content" Value="{Binding OnContent, RelativeSource={RelativeSource TemplatedParent}}" /> </Trigger> <Trigger Property="IsChecked" Value="False"> <Setter TargetName="PART_Switch" Property="Background" Value="{Binding OffBrush, RelativeSource={RelativeSource TemplatedParent}}" /> <Setter TargetName="PART_Thumb" Property="RenderTransform"> <Setter.Value> <TranslateTransform X="0" /> </Setter.Value> </Setter> <Setter TargetName="PART_ContentPresenter" Property="Content" Value="{Binding OffContent, RelativeSource={RelativeSource TemplatedParent}}" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> |
▶ ToggleSwitch.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace TestProject; /// <summary> /// 토글 스위치 /// </summary> public class ToggleSwitch : Control { //////////////////////////////////////////////////////////////////////////////////////////////////// DependencyProperty ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 체크 여부 속성 - IsCheckedProperty /// <summary> /// 체크 여부 속성 /// </summary> public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register ( "IsChecked", typeof(bool), typeof(ToggleSwitch), new PropertyMetadata(false) ); #endregion #region ON 컨텐트 속성 - OnContentProperty /// <summary> /// ON 컨텐트 속성 /// </summary> public static readonly DependencyProperty OnContentProperty = DependencyProperty.Register ( "OnContent", typeof(object), typeof(ToggleSwitch), new PropertyMetadata("ON") ); #endregion #region ON 브러시 속성 - OnBrushProperty /// <summary> /// ON 브러시 /// </summary> public static readonly DependencyProperty OnBrushProperty = DependencyProperty.Register ( "OnBrush", typeof(Brush), typeof(ToggleSwitch), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(33, 150, 243))) ); #endregion #region OFF 컨텐트 속성- OffContentProperty /// <summary> /// OFF 컨텐트 속성 /// </summary> public static readonly DependencyProperty OffContentProperty = DependencyProperty.Register ( "OffContent", typeof(object), typeof(ToggleSwitch), new PropertyMetadata("OFF") ); #endregion #region OFF 브러시 속성 - OffBrushProperty /// <summary> /// OFF 브러시 /// </summary> public static readonly DependencyProperty OffBrushProperty = DependencyProperty.Register ( "OffBrush", typeof(Brush), typeof(ToggleSwitch), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(204, 204, 204))) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 체크 여부 - IsChecked /// <summary> /// 체크 여부 /// </summary> public bool IsChecked { get { return (bool)GetValue(IsCheckedProperty); } set { SetValue(IsCheckedProperty, value); } } #endregion #region ON 컨텐트 - OnContent /// <summary> /// ON 컨텐트 /// </summary> public object OnContent { get { return GetValue(OnContentProperty); } set { SetValue(OnContentProperty, value); } } #endregion #region ON 브러시 - OnBrush /// <summary> /// ON 브러시 /// </summary> public Brush OnBrush { get { return (Brush)GetValue(OnBrushProperty); } set { SetValue(OnBrushProperty, value); } } #endregion #region OFF 컨텐트 - OffContent /// <summary> /// OFF 컨텐트 /// </summary> public object OffContent { get { return GetValue(OffContentProperty); } set { SetValue(OffContentProperty, value); } } #endregion #region OFF 브러시 - OffBrush /// <summary> /// OFF 브러시 /// </summary> public Brush OffBrush { get { return (Brush)GetValue(OffBrushProperty); } set { SetValue(OffBrushProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ToggleSwitch() /// <summary> /// 생성자 /// </summary> static ToggleSwitch() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ToggleSwitch), new FrameworkPropertyMetadata(typeof(ToggleSwitch))); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 템플리트 적용시 처리하기 - OnApplyTemplate() /// <summary> /// 템플리트 적용시 처리하기 /// </summary> public override void OnApplyTemplate() { base.OnApplyTemplate(); DependencyObject dependencyObject = GetTemplateChild("PART_Switch"); if(dependencyObject is Border switchPart) { switchPart.MouseLeftButtonDown += (s, e) => { IsChecked = !IsChecked; e.Handled = true; }; } } #endregion } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" Text="배경 이미지 표시" /> <local:ToggleSwitch Margin="10 0 0 0" VerticalAlignment="Center" OnBrush="Gold" OnContent="" OffContent="" /> </StackPanel> </Window> |