■ ControlTemplate 엘리먼트를 사용해 ListBox 엘리먼트를 정의하는 방법을 보여준다.
▶ 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 |
<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="DisabledControlLightColorKey">#ffe8edf9</Color> <Color x:Key="SelectedBackgroundColorKey">#ffc5cbf9</Color> <Color x:Key="SelectedUnfocusedColorKey">#ffdddddd</Color> <Color x:Key="ControlLightColorKey">White</Color> <Color x:Key="ControlMediumColorKey">#ff7381f9</Color> <Color x:Key="BorderMediumColorKey">#ff888888</Color> <Color x:Key="DisabledBorderLightColorKey">#ffaaaaaa</Color> <Style x:Key="{x:Type ListBox}" TargetType="ListBox"> <Setter Property="MinWidth" Value="120" /> <Setter Property="MinHeight" Value="95" /> <Setter Property="OverridesDefaultStyle" Value="true" /> <Setter Property="SnapsToDevicePixels" Value="true" /> <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" /> <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> <Setter Property="ScrollViewer.CanContentScroll" Value="true" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBox"> <Border Name="border" CornerRadius="2" BorderThickness="1"> <Border.Background> <SolidColorBrush Color="{StaticResource ControlLightColorKey}" /> </Border.Background> <Border.BorderBrush> <SolidColorBrush Color="{StaticResource BorderMediumColorKey}" /> </Border.BorderBrush> <ScrollViewer Margin="0" Focusable="false"> <StackPanel Margin="2" IsItemsHost="True" /> </ScrollViewer> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter TargetName="border" Property="Background"> <Setter.Value> <SolidColorBrush Color="{StaticResource DisabledControlLightColorKey}" /> </Setter.Value> </Setter> <Setter TargetName="border" Property="BorderBrush"> <Setter.Value> <SolidColorBrush Color="{DynamicResource DisabledBorderLightColorKey}" /> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsGrouping" Value="true"> <Setter Property="ScrollViewer.CanContentScroll" Value="false" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="{x:Type ListBoxItem}" TargetType="ListBoxItem"> <Setter Property="SnapsToDevicePixels" Value="true" /> <Setter Property="OverridesDefaultStyle" Value="true" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border Name="border" Padding="2" SnapsToDevicePixels="true"> <Border.Background> <SolidColorBrush Color="Transparent" /> </Border.Background> <VisualStateManager.VisualStateGroups> <VisualStateGroup Name="SelectionStates"> <VisualState Name="Unselected" /> <VisualState Name="Selected"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource SelectedBackgroundColorKey}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState Name="SelectedUnfocused"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"> <EasingColorKeyFrame KeyTime="0" Value="{StaticResource SelectedUnfocusedColorKey}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <ContentPresenter /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <ListBox Width="100" Height="100"> <ListBoxItem>Sunday</ListBoxItem> <ListBoxItem>Monday</ListBoxItem> <ListBoxItem>Tuesday</ListBoxItem> <ListBoxItem>Wednesday</ListBoxItem> <ListBoxItem>Thursday</ListBoxItem> <ListBoxItem>Friday</ListBoxItem> <ListBoxItem>Saturday</ListBoxItem> </ListBox> </Window> |