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