C# WPF, 지난번 포스트 소스에 연결됩니다.

상황에 따라, 표시되는 템플릿이 자동으로 지정되게끔 만들 수 있네요.


SubjectSelectDataTemplate.cs

using System.Windows;

using System.Windows.Controls;

 

namespace WPF_MVVM_SAMPLE01

{

    public class SubjectSelectDataTemplate : DataTemplateSelector

    {

        public override DataTemplate SelectTemplate(object item, DependencyObject container)

        {

            Score score = item as Score;

            FrameworkElement el = container as FrameworkElement;

            return (DataTemplate)el.FindResource(score.SCORE > 60 ? "DataTemplate3" : "DataTemplate4");

        }

    }

}



TemplateSelectWindow.xaml

 

<Window x:Class="WpfApp1.TemplateSelectWindow"

        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:WPF_MVVM_SAMPLE01"

        mc:Ignorable="d"

        Title="TemplateSelectWindow" Height="450" Width="800">

    <Window.DataContext>

        <local:ItemViewModel></local:ItemViewModel>

    </Window.DataContext>

    <Window.Resources>

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

    </Window.Resources>

    <DockPanel>

        <ListBox ItemsSource="{Binding Items}" ItemTemplateSelector="{StaticResource SubjectSelectDataTemplate}" ></ListBox>

    </DockPanel>

</Window>



Dictionary1.xaml

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

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

                    xmlns:local="clr-namespace:WPF_MVVM_SAMPLE01">

    <DataTemplate x:Key="DataTemplate1">

        <TextBlock Text="{Binding SUBJECT}"></TextBlock>

    </DataTemplate>

    <DataTemplate x:Key="DataTemplate2">

        <Grid >

            <Rectangle HorizontalAlignment="Left" Height="30" Width="{Binding SCORE}" StrokeThickness="1" Fill="Red"></Rectangle>

            <TextBlock Text="{Binding SCORE}"></TextBlock>

        </Grid>

    </DataTemplate>

    <DataTemplate x:Key="DataTemplate3">

        <Grid>

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="150"></ColumnDefinition>

                <ColumnDefinition Width="150"></ColumnDefinition>

            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Text="{Binding SUBJECT}"></TextBlock>

            <Rectangle Grid.Column="1" HorizontalAlignment="Left" Height="30" Width="{Binding SCORE}" StrokeThickness="1" Fill="Red"></Rectangle>

            <TextBlock Grid.Column="1" Text="{Binding SCORE}"></TextBlock>   

        </Grid>

    </DataTemplate>

    <DataTemplate x:Key="DataTemplate4">

        <Grid>

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="150"></ColumnDefinition>

                <ColumnDefinition Width="150"></ColumnDefinition>

            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="1" Text="{Binding SUBJECT}"></TextBlock>

            <Rectangle Grid.Column="0" HorizontalAlignment="Left" Height="30" Width="{Binding SCORE}" StrokeThickness="1" Fill="Red"></Rectangle>

            <TextBlock Grid.Column="0" Text="{Binding SCORE}"></TextBlock>

        </Grid>

    </DataTemplate>

 

</ResourceDictionary>



소스는 여기 올려뒀습니다. https://github.com/erith/WPF_SAMPLES/tree/master/WpfApp1

+ Recent posts