앞에서 만들었던 파일인 "RiaSimpleBinding"파일을 복사하여, 복사한 파일을 폴더명만 "IValueConverter"로 바꿔서 프로젝트를 연다. 그 다음 "MainPage.xaml"소스코드를 수정하고
"MainPage.xaml.cs"와 "PostDateConverter.cs"를 코딩한다.






[PostDateConverter.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;

using System.Windows.Data;

 

namespace RiaSimpleBinding

{

    public class PostDateConverter : IValueConverter

    {

        // 12/10/2009 => 2009 12 10 변환

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            // 넘겨져 값을 날짜형으로 변환

            DateTime dt = DateTime.Now;

            if (DateTime.TryParse(value.ToString(), out dt))

            {

                return dt.ToString("yyyyMMdd");

            }

            else

            {

                return "";

            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            throw new NotImplementedException();

        }

    }

}

 

 







[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"

    xmlns:me="clr-namespace:RiaSimpleBinding"

    >

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

        <Grid.Resources>

            <me:PostDateConverter x:Key="pdConverter" />

        </Grid.Resources>

        <StackPanel>

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

            <TextBlock x:Name="lblNum" />

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

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

            <TextBlock Text="{Binding Path=PostDate, Converter={StaticResource pdConverter}}" />

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

        }

    }

}

 

 






[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; }

    }

}

 

 

















Posted by holland14
: