■ 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 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 |
namespace TestProject { /// <summary> /// 장소 /// </summary> public class Place { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 명칭 /// </summary> private string name; /// <summary> /// 주 /// </summary> private string state; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get { return this.name; } set { this.name = value; } } #endregion #region 주 - State /// <summary> /// 주 /// </summary> public string State { get { return this.state; } set { this.state = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Place(name, state) /// <summary> /// 생성자 /// </summary> /// <param name="name">명칭</param> /// <param name="state">주</param> public Place(string name, string state) { this.name = name; this.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 |
<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="MultiDataTrigger 엘리먼트 사용하기" Background="Cornsilk" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:PlaceCollection x:Key="PlaceCollectionKey" /> <Style TargetType="{x:Type 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}"> <StackPanel Orientation="Horizontal" Width="200" Height="30"> <TextBlock VerticalAlignment="Center" Width="150" Padding="5" Text="{Binding Path=Name}" /> <TextBlock VerticalAlignment="Center" Width="50" Padding="5" Text="{Binding Path=State}" /> </StackPanel> </DataTemplate> </Window.Resources> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <ListBox HorizontalAlignment="Center" Background="Honeydew" ItemsSource="{Binding Source={StaticResource PlaceCollectionKey}}" /> </StackPanel> </Window> |
▶ MainWindow.xaml.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 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion } } |