- 코드에서 만들어진 데이터를 컨트롤에 출력
    - 코드 비하인드 페이지에서 작성된 데이터를 단순히 컨트롤에 출력할 때에는 각각의 속성으 로 접근

- 데이터 바인딩
    - Text, JSON, XML, DBMS의 데이터를 Silverlight 영역에 보여주는 방법을 Data Binding이라 한다.






아래그림과 같이 '솔루션 탐색기'에서 "MemoEntity.cs"라는 이름으로 클래스 파일을 추가하고 "MemoEntity.cs"에 아래와 같이 코딩한다.








[MemoEntity.cs]


using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace RiaSimpleBinding

{

    public class MemoEntity

    {

        public int Num { get; set; }

        public string Name { get; set; }

        public DateTime PostDate { get; set; }

    }

}

 

 






[MainPage.xaml]


<UserControl x:Class="RiaSimpleBinding.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>

            <TextBlock Text="직접 바인딩" />

            <TextBlock x:Name="lblNum" />

            <TextBlock Text="XAML 바인딩" />

            <TextBlock Text="{Binding Path=Name}" />

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

        </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;

 

namespace RiaSimpleBinding

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

 

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

        }

 

        void MainPage_Loaded(object sender, RoutedEventArgs e)

        {

            // Memo 데이터 한개 바인딩

            MemoEntity me = new MemoEntity() { Num = 1, Name = "홍길동", PostDate = DateTime.Now };

            //[1] 컨트롤에 직접 바인딩

            this.lblNum.Text = me.Num.ToString();

            //[2] XAML 바인딩식을 사용해서 바인딩

            this.LayoutRoot.DataContext = me;

        }

    }

}

 

 























Posted by holland14
: