■ ControlTemplate 엘리먼트를 사용해 ToolBar 엘리먼트를 정의하는 방법을 보여준다.
▶ 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
<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> <Color x:Key="WindowColorKey">#ffe8edf9</Color> <Color x:Key="DisabledControlDarkColorKey">#ffc5cbf9</Color> <Color x:Key="ControlLightColorKey">White</Color> <Color x:Key="ControlMediumColorKey">#ff7381f9</Color> <Color x:Key="ControlMouseOverColorKey">#ff3843c4</Color> <Color x:Key="ControlPressedColorKey">#ff211aa9</Color> <Color x:Key="BorderLightColorKey">#ffcccccc</Color> <Color x:Key="BorderMediumColorKey">#ff888888</Color> <Color x:Key="BorderDarkColorKey">#ff444444</Color> <Color x:Key="DisabledBorderLightColorKey">#ffaaaaaa</Color> <Style x:Key="ToolBarButtonBaseStyleKey" TargetType="{x:Type ButtonBase}"> <Setter Property="SnapsToDevicePixels" Value="true" /> <Setter Property="OverridesDefaultStyle" Value="true" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ButtonBase}"> <Border Name="border" BorderThickness="1"> <Border.BorderBrush> <LinearGradientBrush StartPoint="0.5 0" EndPoint="0.5 1"> <GradientStop Offset="0" Color="{DynamicResource BorderLightColorKey}" /> <GradientStop Offset="1" Color="{DynamicResource BorderMediumColorKey}" /> </LinearGradientBrush> </Border.BorderBrush> <Border.Background> <LinearGradientBrush StartPoint="0.5 0" EndPoint="0.5 1"> <GradientStop Offset="0" Color="{DynamicResource ControlLightColorKey}" /> <GradientStop Offset="1" Color="{DynamicResource ControlMediumColorKey}" /> </LinearGradientBrush> </Border.Background> <VisualStateManager.VisualStateGroups> <VisualStateGroup Name="CommonStates"> <VisualState Name="Normal" /> <VisualState Name="Pressed"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlPressedColorKey}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState Name="MouseOver"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlMouseOverColorKey}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState Name="Disabled"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledControlDarkColorKey}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup Name="CheckStates"> <VisualState Name="Checked"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlPressedColorKey}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState Name="Unchecked" /> <VisualState Name="Indeterminate" /> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2" RecognizesAccessKey="True" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="{x:Type Button}" BasedOn="{StaticResource ToolBarButtonBaseStyleKey}" /> <Style x:Key="{x:Static ToolBar.ToggleButtonStyleKey}" TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource ToolBarButtonBaseStyleKey}" /> <Style x:Key="{x:Static ToolBar.CheckBoxStyleKey}" TargetType="{x:Type CheckBox}" BasedOn="{StaticResource ToolBarButtonBaseStyleKey}" /> <Style x:Key="{x:Static ToolBar.RadioButtonStyleKey}" TargetType="{x:Type RadioButton}" BasedOn="{StaticResource ToolBarButtonBaseStyleKey}" /> <Style x:Key="{x:Static ToolBar.TextBoxStyleKey}" TargetType="{x:Type TextBox}"> <Setter Property="OverridesDefaultStyle" Value="True" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="AllowDrop" Value="true" /> <Setter Property="KeyboardNavigation.TabNavigation" Value="None" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Border Name="border" BorderThickness="1" Padding="2"> <Border.Background> <SolidColorBrush Color="{DynamicResource ControlLightColorKey}" /> </Border.Background> <Border.BorderBrush> <SolidColorBrush Color="{StaticResource BorderMediumColorKey}" /> </Border.BorderBrush> <VisualStateManager.VisualStateGroups> <VisualStateGroup Name="CommonStates"> <VisualState Name="Normal" /> <VisualState Name="Disabled"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledControlDarkColorKey}" /> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledBorderLightColorKey}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState Name="ReadOnly" /> <VisualState Name="MouseOver" /> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <ScrollViewer Name="PART_ContentHost" Margin="0" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="ToolBarThumbStyleKey" TargetType="{x:Type Thumb}"> <Setter Property="OverridesDefaultStyle" Value="True" /> <Setter Property="Cursor" Value="SizeAll" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Thumb}"> <Border Background="Transparent" SnapsToDevicePixels="True"> <Rectangle Margin="0 2"> <Rectangle.Fill> <DrawingBrush ViewboxUnits="Absolute" Viewbox="0 0 8 8" ViewportUnits="Absolute" Viewport="0 0 4 4" TileMode="Tile"> <DrawingBrush.Drawing> <DrawingGroup> <GeometryDrawing Brush="#aaa" Geometry="M 4 4 L 4 8 L 8 8 L 8 4 z" /> </DrawingGroup> </DrawingBrush.Drawing> </DrawingBrush> </Rectangle.Fill> </Rectangle> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="ToolBarOverflowButtonStyleKey" TargetType="{x:Type ToggleButton}"> <Setter Property="OverridesDefaultStyle" Value="true" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Border Name="border" CornerRadius="0 3 3 0" SnapsToDevicePixels="true"> <Border.Background> <LinearGradientBrush StartPoint="0.5 0" EndPoint="0.5 1"> <GradientStop Offset="0" Color="#00000000" /> <GradientStop Offset="1" /> </LinearGradientBrush> </Border.Background> <VisualStateManager.VisualStateGroups> <VisualStateGroup Name="CommonStates"> <VisualState Name="Normal" /> <VisualState Name="Pressed"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlPressedColorKey}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState Name="MouseOver"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlMouseOverColorKey}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState Name="Disabled"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledBorderLightColorKey}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid> <Path VerticalAlignment="Bottom" Margin="2 3" Fill="Black" Data="M -0.5 3 L 5.5 3 L 2.5 6 Z" /> <ContentPresenter /> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="{x:Type ToolBar}" TargetType="{x:Type ToolBar}"> <Setter Property="OverridesDefaultStyle" Value="true" /> <Setter Property="SnapsToDevicePixels" Value="true" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToolBar}"> <Border Name="border" CornerRadius="2" BorderThickness="1"> <Border.BorderBrush> <LinearGradientBrush StartPoint="0 0" EndPoint="0 1"> <LinearGradientBrush.GradientStops> <GradientStopCollection> <GradientStop Offset="0.0" Color="{DynamicResource BorderMediumColorKey}" /> <GradientStop Offset="1.0" Color="{DynamicResource BorderDarkColorKey}" /> </GradientStopCollection> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Border.BorderBrush> <Border.Background> <LinearGradientBrush StartPoint="0 0" EndPoint="0 1"> <LinearGradientBrush.GradientStops> <GradientStopCollection> <GradientStop Offset="0.0" Color="#fff" /> <GradientStop Offset="1.0" Color="{DynamicResource ControlMediumColorKey}" /> </GradientStopCollection> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Border.Background> <DockPanel> <ToggleButton DockPanel.Dock="Right" Style="{StaticResource ToolBarOverflowButtonStyleKey}" ClickMode="Press" IsEnabled="{TemplateBinding HasOverflowItems}" IsChecked="{Binding IsOverflowOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> <Popup Placement="Bottom" AllowsTransparency="true" PopupAnimation="Slide" StaysOpen="false" Focusable="false" IsOpen="{Binding IsOverflowOpen, RelativeSource={RelativeSource TemplatedParent}}"> <Border BorderThickness="1"> <Border.BorderBrush> <SolidColorBrush Color="{DynamicResource BorderMediumColorKey}" /> </Border.BorderBrush> <Border.Background> <LinearGradientBrush StartPoint="0.5 0" EndPoint="0.5 1"> <GradientStop Color="{DynamicResource ControlLightColorKey}" /> <GradientStop Offset="1" Color="{DynamicResource ControlMediumColorKey}" /> </LinearGradientBrush> </Border.Background> <ToolBarOverflowPanel Name="PART_ToolBarOverflowPanel" FocusVisualStyle="{x:Null}" Margin="2" WrapWidth="200" Focusable="true" KeyboardNavigation.TabNavigation="Cycle" KeyboardNavigation.DirectionalNavigation="Cycle" /> </Border> </Popup> </ToggleButton> <Thumb Name="toolBarThumb" Style="{StaticResource ToolBarThumbStyleKey}" Width="10" /> <ToolBarPanel Name="PART_ToolBarPanel" Margin="0 1 2 2" IsItemsHost="true" /> </DockPanel> </Border> <ControlTemplate.Triggers> <Trigger Property="IsOverflowOpen" Value="true"> <Setter TargetName="toolBarThumb" Property="IsEnabled" Value="false" /> </Trigger> <Trigger Property="ToolBarTray.IsLocked" Value="true"> <Setter TargetName="toolBarThumb" Property="Visibility" Value="Collapsed" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="{x:Type ToolBarTray}" TargetType="{x:Type ToolBarTray}"> <Setter Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0 0" EndPoint="1 0"> <LinearGradientBrush.GradientStops> <GradientStopCollection> <GradientStop Offset="0.0" Color="#fff" /> <GradientStop Offset="1.0" Color="{DynamicResource WindowColorKey}" /> </GradientStopCollection> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Setter.Value> </Setter> </Style> </Window.Resources> <DockPanel LastChildFill="False"> <ToolBarTray DockPanel.Dock="Top"> <ToolBar> <Button Content="Button" /> <CheckBox Content="CheckBox" /> <TextBox Text="TextBox" /> <TextBox Text="Disabled TextBox" IsEnabled="False" /> <ToggleButton Content="ToggleButton" /> <RadioButton IsChecked="True" Content="RadioButton 1" /> <RadioButton IsChecked="True" Content="RadioButton 1" /> <Button Content="Button" ToolBar.OverflowMode="Always" /> <CheckBox Content="CheckBox" ToolBar.OverflowMode="Always" /> <TextBox Text="TextBox" ToolBar.OverflowMode="Always" /> </ToolBar> </ToolBarTray> </DockPanel> </Window> |