첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ AuctionItemDataTemplateSelector.cs

using System.Windows;
using System.Windows.Controls;

namespace TestProject
{
    /// <summary>
    /// 경매 항목 데이터 템플리트 선택자
    /// </summary>
    public class AuctionItemDataTemplateSelector : DataTemplateSelector
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 템플리트 선택하기 - SelectTemplate(item, container)

        /// <summary>
        /// 템플리트 선택하기
        /// </summary>
        /// <param name="item">항목</param>
        /// <param name="container">컨테이너</param>
        /// <returns>템플리트</returns>
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;

            if(element != null && item != null && item is AuctionItem)
            {
                AuctionItem auctionItem = item as AuctionItem;

                Window window = Application.Current.MainWindow;

                switch(auctionItem.FeatureType)
                {
                    case FeatureType.None  : return element.FindResource("NoneDataTemplateKey" ) as DataTemplate;
                    case FeatureType.Color : return element.FindResource("ColorDataTemplateKey") as DataTemplate;
                }
            }

            return null;
        }

        #endregion
    }
}

 

728x90

 

▶ 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="DateTemplateSelector 클래스 : 데이터 템플리트 셀렉터 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <Style x:Key="SmallTitleTextBlockStyleKey" TargetType="{x:Type TextBlock}">
            <Setter Property="FontWeight"          Value="Bold"    />
            <Setter Property="Foreground"          Value="DimGray" />
            <Setter Property="VerticalAlignment"   Value="Center"  />
            <Setter Property="HorizontalAlignment" Value="Right"   />
        </Style>
        <Style x:Key="TextTextBlockStyleKey" TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground" Value="#333333" />
        </Style>
        <Style x:Key="ColorTextBlockStyleKey" TargetType="{x:Type TextBlock}">
            <Setter Property="FontFamily"          Value="Segoe"   />
            <Setter Property="FontWeight"          Value="Bold"    />
            <Setter Property="FontSize"            Value="18"      />
            <Setter Property="Foreground"          Value="#787070" />
            <Setter Property="VerticalAlignment"   Value="Center"  />
            <Setter Property="HorizontalAlignment" Value="Left"    />
        </Style>
        <DataTemplate x:Key="NoneDataTemplateKey">
            <Border
                Margin="3"
                Width="400"
                BorderThickness="1"
                BorderBrush="Gray"
                Padding="7">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"  />
                        <ColumnDefinition Width="130" />
                        <ColumnDefinition Width="*"   />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="1"
                        Style="{StaticResource SmallTitleTextBlockStyleKey}"
                        Margin="0 0 8 0">
                        Description :
                    </TextBlock>
                    <TextBlock Grid.Row="0" Grid.Column="2"
                        Style="{StaticResource TextTextBlockStyleKey}"
                        Text="{Binding Path=Description}" />
                    <TextBlock Grid.Row="1" Grid.Column="1"
                        Style="{StaticResource SmallTitleTextBlockStyleKey}"
                        Margin="0 0 8 0">
                        Current Price :
                    </TextBlock>
                    <StackPanel Grid.Row="1" Grid.Column="2"
                        Orientation="Horizontal">
                        <TextBlock
                            Style="{StaticResource TextTextBlockStyleKey}"
                            Text="$" />
                        <TextBlock
                            Style="{StaticResource TextTextBlockStyleKey}"
                            Text="{Binding Path=CurrentPrice}" />
                    </StackPanel>
                </Grid>
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="ColorDataTemplateKey">
            <Border
                Margin="3"
                Width="400"
                BorderBrush="Orange"
                BorderThickness="3"
                Padding="5">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"  />
                        <ColumnDefinition Width="130" />
                        <ColumnDefinition Width="*"   />
                    </Grid.ColumnDefinitions>
                    <Polygon Name="star" Grid.Row="0" Grid.Column="0" Grid.RowSpan="4"
                        Width="20"
                        Height="20"
                        Stroke="Black"
                        StrokeThickness="1"
                        StrokeLineJoin="Round"
                        Fill="Yellow"
                        Stretch="Fill"
                        Points="9 2 11 7 17 7 12 10 14 15 9 12 4 15 6 10 1 7 7 7"
                        Visibility="Visible" />
                    <TextBlock Grid.Row="0" Grid.Column="1"
                        Style="{StaticResource SmallTitleTextBlockStyleKey}"
                        Margin="0 0 8 0">
                        Description :
                    </TextBlock>
                    <TextBlock Grid.Row="0" Grid.Column="2"
                        Style="{StaticResource ColorTextBlockStyleKey}"
                        Text="{Binding Path=Description}" />
                    <TextBlock Grid.Row="1" Grid.Column="1"
                        Style="{StaticResource SmallTitleTextBlockStyleKey}"
                        Margin="0 0 8 0">
                        Current Price :
                    </TextBlock>
                    <StackPanel Grid.Row="1" Grid.Column="2"
                        Orientation="Horizontal">
                        <TextBlock
                            Style="{StaticResource ColorTextBlockStyleKey}"
                            Text="$" />
                        <TextBlock
                            Style="{StaticResource ColorTextBlockStyleKey}"
                            Text="{Binding Path=CurrentPrice}" />
                    </StackPanel>
                </Grid>
            </Border>
        </DataTemplate>
        <local:AuctionItemDataTemplateSelector x:Key="AuctionItemDataTemplateSelectorKey" />
        <local:AuctionItemCollection x:Key="AuctionItemCollectionKey" />
        <CollectionViewSource x:Key="CollectionViewSourceKey"
            Source="{StaticResource AuctionItemCollectionKey}" />
        <ControlTemplate x:Key="ScrollTemplateKey">
            <ScrollViewer>
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <Border
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            BorderBrush="Black"
            BorderThickness="1">
            <ItemsControl
                Width="430"
                Height="300"
                Template="{StaticResource ScrollTemplateKey}"
                ItemTemplateSelector="{StaticResource AuctionItemDataTemplateSelectorKey}"
                ItemsSource="{Binding Source={StaticResource CollectionViewSourceKey}}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Border>
    </Grid>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요