WPF, XAML

(C#/WPF 기초교육)WPF트리거, 데이터트리거 예제, DataTrigger, MultiDataTrigger 실습예제

FSP 0 304 02.23 17:43

(C#/WPF 기초교육)WPF트리거, 데이터트리거 예제, DataTrigger, MultiDataTrigger 실습예제 

 

www.topcredu.co.kr 


n  ListBox 컨트롤에 사원들을 출력하는데 DataTrigger를 사용하여 특정 부서 사원들을 다르게 표시하고 MultiDataTrigger를 이용하여 사번과 이름이 지정된 값인 경우 배경색을 다르게 표시하는 예제이다.

n  실행화면 

 

 

n  [Emp.cs]

using System.Collections.ObjectModel;

 

namespace WpfApp1

    public class Emp

    {

        private int _Empno;

        private string _Ename;

        private int _Deptno;

 

        public int Empno

        {

            get { return _Empno; }

            set { _Empno = value; }

        }

 

        public string Ename

        {

            get { return _Ename; }

            set { _Ename= value; }

        }

 

        public int Deptno

        {

            get { return _Deptno; }

            set { _Deptno = value; }

        }

 

        public Emp(int Empno, string Ename, int Deptno)

        {

            this.Empno = Empno;

            this.Ename = Ename;

            this.Deptno = Deptno;

        }

    }

 

    public class Emps : ObservableCollection<Emp>

    {

        public Emps()

        {

            Add(new Emp(1001, "SMITH", 10));

            Add(new Emp(1002, "KING", 10));

            Add(new Emp(1003, "ALLEN", 20));

            Add(new Emp(1004, "TIGER", 10));

            Add(new Emp(1005, "SCOTT", 20));

            Add(new Emp(1006, "JHON", 10));

        }

    }

}

 

n  [MainWindow.xaml]

<Window x:Class="WpfApp1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApp1"

        mc:Ignorable="d"

        Title="MainWindow" Height="250" Width="450">

    <Window.Resources>

        <local:Emps x:Key="Emps"/>

        <Style TargetType="{x:Type ListBoxItem}">

            <Style.Triggers>

                <DataTrigger Binding="{Binding Path=Deptno}" Value="10">

                    <Setter Property="Background" Value="Yellow" />

                </DataTrigger>

                <DataTrigger Binding="{Binding Path=Deptno}" Value="20">

                    <Setter Property="Background" Value="Green" />

                </DataTrigger>

                <MultiDataTrigger>

                    <MultiDataTrigger.Conditions>

                        <Condition Binding="{Binding Path=Empno}" Value="1001" />

                        <Condition Binding="{Binding Path=Ename}" Value="SMITH" />

                    </MultiDataTrigger.Conditions>

                    <MultiDataTrigger.Setters>

                        <Setter Property="Background" Value="RED" />

                    </MultiDataTrigger.Setters>

                </MultiDataTrigger>

            </Style.Triggers>

        </Style>

 

        <DataTemplate DataType="{x:Type local:Emp}">

            <Canvas Width="260" Height="20">

                <TextBlock FontSize="12" Width="130" Canvas.Left="0" Text="{Binding Path=Empno}"/>

                <TextBlock FontSize="12" Width="130"  Canvas.Left="130" Text="{Binding Path=Ename}"/>

            </Canvas>

        </DataTemplate>

    </Window.Resources>

 

    <StackPanel>

        <TextBlock FontSize="18" FontWeight="Bold" HorizontalAlignment="Center">데이터 트리거 샘플</TextBlock>

        <ListBox Width="300" HorizontalAlignment="Center" Background="White"

                 ItemsSource="{Binding Source={StaticResource Emps}}"/>

    </StackPanel>

</Window>

 

 

 

Comments