퇴근5분전


 오늘 만든 폼에 사용해야 될 객체중 하나로 사용해본 후 기록함.

    // 정의
    public interface IValueConverter
    {
             object Convert(object value, Type targetType, object parameter, CultureInfo culture);
             object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
    }

 xaml에서 표현하는 컨트롤이 checkbox이고  내부 데이타(DB코드값)는 숫자타입 0 : false, 0 이 아니면 true라고 한다면...

[ValueConversion( typeof(int), typeof(bool))]
 public class IntToBoolenValueConverter : IValueConverter
    {
        #region IValueConverter 멤버
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int i = System.Convert.ToInt32(value);
            return i != 0;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool b = System.Convert.ToBoolean(value);
            return b ? 1 : 0;
        }
        #endregion
    }

위와 같이 구현한다.

추가:  ValueConversion 속성순서처럼
int -> bool은 convert메서드에서
bool -> int는 ConvertBack 메서드에서 각각 구현순서로 기억하면 편하다
.




< 아래 소스 실행화면 >


/********************************************************************************************************
.xaml 내용
<Window x:Class="IValueConverterDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Me="clr-namespace:IValueConverterDemo"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
    
        <ResourceDictionary>             
            <Me:IntToBoolenValueConverter x:Key="intToBoolenValueConverter" ></Me:IntToBoolenValueConverter>
            <DataTemplate x:Key="ListBoxItemTemplate" >
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding Path=dtChecked, Converter={StaticResource intToBoolenValueConverter}}"></CheckBox>
                    <TextBlock Text="{Binding Path=dtText}"></TextBlock>                   
                </StackPanel>
            </DataTemplate>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <ListBox Name="LstBox" ItemsSource="{Binding}" ItemTemplate="{StaticResource ListBoxItemTemplate}" ></ListBox>
    </Grid>
</Window>

.cs 내용

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace IValueConverterDemo
{
    /// <summary>
    /// Window1.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("dtChecked");
            dt.Columns.Add("dtText");

            dt.Rows.Add( 1, "0 to False , 1 to True");
          dt.Rows.Add( 0, "0 to False , 1 to True");

            LstBox.DataContext = dt;
        }
    }

    [ValueConversion( typeof(int), typeof(bool))]
    public class IntToBoolenValueConverter : IValueConverter
    {
        #region IValueConverter 멤버
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int i = System.Convert.ToInt32(value);
            return i != 0;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool b = System.Convert.ToBoolean(value);
            return b ? 1 : 0;
        }
        #endregion
    }

}