[C#/WPF/.NET8] ListBox 엘리먼트 : Style 속성을 사용해 글래스 효과 설정하기
■ ListBox 엘리먼트의 Style 속성을 사용해의 리스트 박스에 글래스 효과를 설정하는 방법을 보여준다. ▶ 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 |
<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" Background="#2d2d2d" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Style x:Key="GlassListBoxItemStyleKey" TargetType="ListBoxItem"> <Setter Property="BorderThickness" Value="1" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="Padding" Value="4 1" /> <Setter Property="Background" Value="Transparent" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment , RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border Name="border" Margin="2" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" > <Setter.Value> <LinearGradientBrush StartPoint="0 0" EndPoint="0 1"> <GradientStop Offset="0" Color="#20ffffff" /> <GradientStop Offset="1" Color="#10ffffff" /> </LinearGradientBrush> </Setter.Value> </Setter> <Setter TargetName="border" Property="BorderBrush" Value="#50ffffff" /> </Trigger> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="border" Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0 0" EndPoint="0 1"> <GradientStop Offset="0" Color="#40ffffff" /> <GradientStop Offset="1" Color="#20ffffff" /> </LinearGradientBrush> </Setter.Value> </Setter> <Setter TargetName="border" Property="BorderBrush" Value="#80ffffff" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="GlassListBoxStyleKey" TargetType="ListBox"> <Setter Property="ItemContainerStyle" Value="{StaticResource GlassListBoxItemStyleKey}"/> <Setter Property="BorderThickness" Value="1" /> <Setter Property="BorderBrush" Value="#20ffffff" /> <Setter Property="Background" Value="Transparent" /> <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" /> <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> </Style> </Window.Resources> <ListBox Style="{StaticResource GlassListBoxStyleKey}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="300" Height="300" Foreground="White"> <ListBoxItem>항목 01</ListBoxItem> <ListBoxItem>항목 02</ListBoxItem> <ListBoxItem>항목 03</ListBoxItem> <ListBoxItem>항목 04</ListBoxItem> <ListBoxItem>항목 05</ListBoxItem> <ListBoxItem>항목 06</ListBoxItem> <ListBoxItem>항목 07</ListBoxItem> <ListBoxItem>항목 08</ListBoxItem> <ListBoxItem>항목 09</ListBoxItem> <ListBoxItem>항목 10</ListBoxItem> </ListBox> </Window> |
TestProject.zip