■ DataTrigger/MultiDataTrigger 엘리먼트를 사용해 데이터 트리거를 설정하는 방법을 보여준다.
▶ Place.cs
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 |
namespace TestProject { /// <summary> /// 장소 /// </summary> public class Place { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get; set; } #endregion #region 주 - State /// <summary> /// 주 /// </summary> public string State { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Place(name, state) /// <summary> /// 생성자 /// </summary> /// <param name="name">명칭</param> /// <param name="state">주</param> public Place(string name, string state) { Name = name; State = state; } #endregion } } |
▶ PlaceCollection.cs
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 |
using System.Collections.ObjectModel; namespace TestProject { /// <summary> /// 장소 컬렉션 /// </summary> public class PlaceCollection : ObservableCollection<Place> { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PlaceCollection() /// <summary> /// 생성자 /// </summary> public PlaceCollection() { Add(new Place("Bellevue" , "WA")); Add(new Place("Gold Beach" , "OR")); Add(new Place("Kirkland" , "WA")); Add(new Place("Los Angeles" , "CA")); Add(new Place("Portland" , "ME")); Add(new Place("Portland" , "OR")); Add(new Place("Redmond" , "WA")); Add(new Place("San Diego" , "CA")); Add(new Place("San Francisco", "CA")); Add(new Place("San Jose" , "CA")); Add(new Place("Seattle" , "WA")); } #endregion } } |
▶ 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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:PlaceCollection x:Key="PlaceCollectionKey" /> <Style TargetType="ListBoxItem"> <Style.Triggers> <DataTrigger Binding="{Binding Path=State}" Value="WA"> <Setter Property="Foreground" Value="Red" /> </DataTrigger> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding Path=Name}" Value="Portland" /> <Condition Binding="{Binding Path=State}" Value="OR" /> </MultiDataTrigger.Conditions> <Setter Property="Background" Value="Cyan" /> </MultiDataTrigger> </Style.Triggers> </Style> <DataTemplate DataType="{x:Type local:Place}"> <Canvas Width="160" Height="25"> <TextBlock Width="130" Text="{Binding Path=Name}" /> <TextBlock Canvas.Left="130" Width="30" Text="{Binding Path=State}" /> </Canvas> </DataTemplate> </Window.Resources> <StackPanel VerticalAlignment="Center"> <TextBlock HorizontalAlignment="Center" Margin="10" FontSize="18" FontWeight="Bold"> Data Trigger Sample </TextBlock> <ListBox HorizontalAlignment="Center" Width="180" Background="Honeydew" ItemsSource="{Binding Source={StaticResource PlaceCollectionKey}}" /> </StackPanel> </Window> |