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

WPF에서 MVVM으로 데이터의 출력형태 템플릿을 미리 확인할 수 있습니다,



직장 동료분에게 잠깐 블렌드 사용법을 배웠는데, 기능이 막강합니다. WPF 프로그램 디자인시에는 필수 프로그램이네요. DataContext 설정시, 디자인 타임에, 설정된 템플릿의 형태를 미리 확인할 수 있어 참 편리합니다.



BLEND 실행화면


주요 소스입니다.


MainWindow.xaml

<Window x:Class="WPF_MVVM_SAMPLE01.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:WPF_MVVM_SAMPLE01"

        mc:Ignorable="d"       

        Title="MainWindow" Height="250" Width="350"       

        >

    <Window.DataContext>

        <local:ItemViewModel></local:ItemViewModel>

    </Window.DataContext>

   

    <DockPanel>

        <ListView Width="350" ItemsSource="{Binding Items}" ItemTemplate="{DynamicResource DataTemplate1}" DockPanel.Dock="Top">

            <ListView.View>

                <GridView>

                    <GridViewColumn Header="SUBJECT" Width="150" CellTemplate="{StaticResource DataTemplate1}" ></GridViewColumn>

                    <GridViewColumn Header="SCORE" Width="180"  CellTemplate="{StaticResource DataTemplate2}"></GridViewColumn>

                </GridView>

            </ListView.View>

        </ListView>

    </DockPanel>

</Window>


Dicionary1.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 Height="30" Width="{Binding SCORE}" StrokeThickness="1" Fill="Red"></Rectangle>

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

        </Grid>

    </DataTemplate>

 

</ResourceDictionary>



ItemViewModel.cs


public class ItemViewModel

    {

        private readonly ScoreCollection items;

 

        public ItemViewModel()

        {

            this.items = new ScoreCollection();

        }

 

        public ScoreCollection Items

        {

            get { return this.items; }

        }

    }


ScoreCollection.cs

using System.Collections.ObjectModel;

 

namespace WPF_MVVM_SAMPLE01

{

    public class ScoreCollection : ObservableCollection<Score>

    {

        public ScoreCollection()

        {

            Add(new Score() { SUBJECT = "Englsh", SCORE = 95 });

            Add(new Score() { SUBJECT = "Mathmatics", SCORE = 55 });

            Add(new Score() { SUBJECT = "History", SCORE = 65 });

        }

    }

 

    public class Score

    {

        public string SUBJECT { get; set; }

        public int SCORE { get; set; }

    }

}



소스: https://github.com/erith/WPF_SAMPLES/tree/master/WpfApp1



+ Recent posts