■ ControlTemplate 엘리먼트를 사용해 Button 엘리먼트를 정의하는 방법을 보여준다.
▶ 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 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 224 225 226 227 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <SolidColorBrush x:Key="BackgroundBorderDisabledBackgroundBrushKey" Color="#ff787878" /> <SolidColorBrush x:Key="BackgroundBorderDisabledBorderBrushKey" Color="#aaa" /> <SolidColorBrush x:Key="HoverBorderBackgroundBrushKey" Color="#ff393939" /> <LinearGradientBrush x:Key="HoverShineBorderBackgroundBrushKey" StartPoint="0.5 0.042" EndPoint="0.5 0.971"> <GradientStop Offset="0" Color="#4cffffff" /> <GradientStop Offset="1" Color="#26ffffff" /> <GradientStop Offset="0.467" Color="#26ffffff" /> <GradientStop Offset="0.475" Color="#00ffffff" /> <GradientStop Offset="0.856" Color="#00ffffff" /> </LinearGradientBrush> <LinearGradientBrush x:Key="PressedBorderBackgroundBrushKey" StartPoint="0.5 0.042" EndPoint="0.5 0.971"> <GradientStop Offset="0" Color="#4c000000" /> <GradientStop Offset="1" Color="#26ffffff" /> <GradientStop Offset="0.467" Color="#4c000000" /> <GradientStop Offset="0.479" Color="#26ffffff" /> </LinearGradientBrush> <SolidColorBrush x:Key="ShineBorderDefaultBorderBrushKey" Color="#ff9bb1c5" /> <LinearGradientBrush x:Key="ShineBoderBackgroundBrushKey" StartPoint="0.5 0.042" EndPoint="0.5 0.971"> <GradientStop Offset="0" Color="#59ffffff" /> <GradientStop Offset="1" Color="#00ffffff" /> <GradientStop Offset="0.467" Color="#26ffffff" /> <GradientStop Offset="0.475" Color="#00ffffff" /> </LinearGradientBrush> <ControlTemplate x:Key="ButtonTemplateKey" TargetType="{x:Type Button}"> <ControlTemplate.Resources> <Storyboard x:Key="HoverOnStoryboardKey"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="hovershineBorder" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00"> <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" /> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="hoverBorder" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00"> <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" /> </DoubleAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="hoverOffStoryboardKey"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="hovershineBorder" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00"> <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" /> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="hoverBorder" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00"> <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" /> </DoubleAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="pressedOnStoryboardKey"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="pressedBorder" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00"> <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" /> </DoubleAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="pressedOffStoryboardKey"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="pressedBorder" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00"> <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" /> </DoubleAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="focusedOnStoryboardKey"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="focusBorder" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00"> <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" /> </DoubleAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="focussedOffStoryboardKey"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="focusBorder" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00"> <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" /> </DoubleAnimationUsingKeyFrames> </Storyboard> </ControlTemplate.Resources> <Grid Name="grid"> <Border Name="backgroundBorder" CornerRadius="3" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" /> <Border Name="hoverBorder" CornerRadius="3" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{StaticResource HoverBorderBackgroundBrushKey}" Opacity="0" /> <Border Name="hovershineBorder" CornerRadius="3" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{StaticResource HoverShineBorderBackgroundBrushKey}" Opacity="0" /> <Border Name="pressedBorder" CornerRadius="3" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{StaticResource PressedBorderBackgroundBrushKey}" Opacity="0" /> <Border Name="shineBorder" CornerRadius="3" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{StaticResource ShineBoderBackgroundBrushKey}" Opacity="1" /> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" /> <Border Name="focusBorder" Margin="1 1 1 1" CornerRadius="3 3 3 3" BorderThickness="1" BorderBrush="{StaticResource HoverShineBorderBackgroundBrushKey}" IsHitTestVisible="false" Opacity="0" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsDefault" Value="True"> <Setter TargetName="shineBorder" Property="BorderBrush" Value="{DynamicResource ShineBorderDefaultBorderBrushKey}" /> </Trigger> <Trigger Property="IsKeyboardFocused" Value="true"> <Trigger.ExitActions> <BeginStoryboard Storyboard="{StaticResource focussedOffStoryboardKey}" /> </Trigger.ExitActions> <Trigger.EnterActions> <BeginStoryboard Storyboard="{StaticResource focusedOnStoryboardKey}" /> </Trigger.EnterActions> </Trigger> <Trigger Property="IsMouseOver" Value="true"> <Trigger.ExitActions> <BeginStoryboard Storyboard="{StaticResource hoverOffStoryboardKey}" /> </Trigger.ExitActions> <Trigger.EnterActions> <BeginStoryboard Storyboard="{StaticResource HoverOnStoryboardKey}" /> </Trigger.EnterActions> </Trigger> <Trigger Property="IsPressed" Value="true"> <Trigger.ExitActions> <BeginStoryboard Storyboard="{StaticResource pressedOffStoryboardKey}" /> </Trigger.ExitActions> <Trigger.EnterActions> <BeginStoryboard Storyboard="{StaticResource pressedOnStoryboardKey}" /> </Trigger.EnterActions> </Trigger> <Trigger Property="IsEnabled" Value="true" /> <Trigger Property="IsEnabled" Value="false"> <Setter TargetName="backgroundBorder" Property="Background" Value="{DynamicResource BackgroundBorderDisabledBackgroundBrushKey}" /> <Setter TargetName="shineBorder" Property="BorderBrush" Value="{DynamicResource BackgroundBorderDisabledBorderBrushKey}" /> <Setter TargetName="grid" Property="Opacity" Value="0.5" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> <Style x:Key="FocusVisualStyleKey"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <Border> <Rectangle Margin="2" StrokeThickness="1" StrokeDashArray="1 2" Stroke="#60000000" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <SolidColorBrush x:Key="ButtonBorderBrushKey" Color="#ff333333" /> <SolidColorBrush x:Key="ButtonBackgroundBrushKey" Color="#ff595959" /> <SolidColorBrush x:Key="ButtonForegroundBrushKey" Color="#ffffffff" /> <Style TargetType="{x:Type Button}" BasedOn="{x:Null}"> <Setter Property="Template" Value="{DynamicResource ButtonTemplateKey}" /> <Setter Property="FocusVisualStyle" Value="{DynamicResource FocusVisualStyleKey}" /> <Setter Property="BorderBrush" Value="{DynamicResource ButtonBorderBrushKey}" /> <Setter Property="Background" Value="{DynamicResource ButtonBackgroundBrushKey}" /> <Setter Property="Foreground" Value="{DynamicResource ButtonForegroundBrushKey}" /> </Style> </Window.Resources> <Button HorizontalAlignment="Center" VerticalAlignment="Center" Padding="10" Foreground="White" Content="선택" /> </Window> |