.NET프로그래밍/Silverlight 3.0

DataGrid 컨트롤로 데이터 출력

holland14 2009. 12. 11. 09:54




- WinForm/ASP.NET에서 제공되는 Grid컨트롤보다는 기능의 아쉬움이 있다.

- DataGrid 사용
    - 왼쪽 도구 상자에서 DataGrid 끌어서 XAML에 등록
        - System.Windows.Controls.Data 어셈블리 등록 필요






[MainPage.xaml]


<UserControl

xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="RiaDataGrid.MainPage"

    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"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="400">

 

    <Grid x:Name="LayoutRoot" Background="White">

        <StackPanel>

            <my:DataGrid x:Name="ctlMemoList" AutoGenerateColumns="True"></my:DataGrid>

            <my:DataGrid x:Name="ctlMemoList2" AutoGenerateColumns="False">

                <my:DataGrid.Columns>

                    <my:DataGridTextColumn Header="번호" Binding="{Binding Num}"></my:DataGridTextColumn>

                    <my:DataGridTemplateColumn Header="이름">

                        <my:DataGridTemplateColumn.CellTemplate>

                            <DataTemplate>

                                <TextBlock Text="{Binding Name}" Foreground="Blue" />

                            </DataTemplate>

                        </my:DataGridTemplateColumn.CellTemplate>

                    </my:DataGridTemplateColumn>

                </my:DataGrid.Columns>

            </my:DataGrid>

        </StackPanel>

    </Grid>

</UserControl>

 

 








[MainPage.xaml.cs]


using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Collections.ObjectModel;

 

namespace RiaDataGrid

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

 

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

        }

 

        void MainPage_Loaded(object sender, RoutedEventArgs e)

        {

            FuncList();

            FuncObservable();

        }

 

        private void FuncObservable()

        {

            ObservableCollection<Memo> lst = new ObservableCollection<Memo>();

 

            lst.Add(new Memo() { Num = 1, Name = "홍길동" });

            lst.Add(new Memo() { Num = 2, Name = "백두산" });

            lst.Add(new Memo() { Num = 3, Name = "한라산" });

 

            this.ctlMemoList2.ItemsSource = lst;

        }

 

        private void FuncList()

        {

            List<Memo> lst = new List<Memo>();

 

            lst.Add(new Memo() { Num = 1, Name = "홍길동" });

            lst.Add(new Memo() { Num = 2, Name = "백두산" });

            lst.Add(new Memo() { Num = 3, Name = "한라산" });

 

            this.ctlMemoList.ItemsSource = lst;

        }

    }

 

    public class Memo

    {

        public int Num { get; set; }

        public string Name { get; set; }

    }

}