■ DataTemplate 엘리먼트의 Triggers 속성을 사용해 DataTrigger를 사용하는 방법을 보여준다.
▶ 예제 코드 (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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="리스트 박스 테스트" Width="600" Height="450" Loaded="mainWindow_Loaded"> <Grid> <ListBox Name="listBox" Width="200" Height="300" SelectionMode="Single"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Name="itemImage" Margin="0 0 5 0" Width="16" Height="16" VerticalAlignment="Center" Stretch="None" Source="{Binding Icon}" /> <TextBlock VerticalAlignment="Center" Text="{Binding Text}" /> </StackPanel> <DataTemplate.Triggers> <DataTrigger Binding="{Binding Icon}" Value="{x:Null}"> <Setter TargetName="itemImage" Property="Visibility" Value="Collapsed" /> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> |