첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

■ DataTrigger/MultiDataTrigger 엘리먼트를 사용해 데이터 트리거를 설정하는 방법을 보여준다.

TestProject.zip
0.01MB

▶ Place.cs

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

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

<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>
728x90
그리드형(광고전용)
Posted by icodebroker
,