SPATIUM Mobile
주소복사
About Operating System Languages Tools Favorites Notice Visit TEST  
     Android (3)
     Linux (1)
     MacOS (1)
     OS (1)
     Solaris10 (15)
     Windows (1)
     Windows Server (2)
     Windows XP (3)
   ID  
   Password  
  |  
  Location United States
  IP Address 3.15.221.67
2024. 04
123456
78910111213
14151617181920
212223
24
252627
282930
Category  Languages, WPF
Writer 김태우 Date 2013-08-06 12:16:38 Visit 3659
Binding Converter : IValueConverter
 

Binding Converter : IValueConverter 

 
데이터 바인딩에서 바인딩 값을 변환하기 위하여 사용
 
 
 

1. 기본적인 사용

 
[ xmal ]
NameSpace정의 - xmal 최상위 태그에 정의
 
    xmlns:src="clr-namespace:ProjectTest.Converters"
 
Resources에 정의

<src:DateConverter x:Key="dateConverter"/>
 
xmal에서 converter 사용

<TextBlock Grid.Row="2" Grid.Column="0" Margin="0,0,8,0"
           Name="startDateTitle"
           Style="{StaticResource smallTitleStyle}">Start Date:</TextBlock>
<TextBlock Name="StartDateDTKey" Grid.Row="2" Grid.Column="1" 
    Text="{Binding Path=StartDate, Converter={StaticResource dateConverter}}" 
    Style="{StaticResource textStyleTextBlock}"/>
 
 
[ cs ]

    public class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            return date.ToShortDateString();
        }
		
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string strValue = value as string;
            DateTime resultDateTime;
            if (DateTime.TryParse(strValue, out resultDateTime))
            {
                return resultDateTime;
            }
            return DependencyProperty.UnsetValue;
        }
    }
 
 
 

2. Style Converter

 
[ xmal ]
NameSpace정의 - xmal 최상위 태그에 정의
 
    xmlns:Converters="clr-namespace:ProjectTest.Converters"
 
Resources에 정의 - 예) UserControl

    <UserControl.Resources>
        <Converters:StyleConverter x:Key="StyleConverter" />
    </UserControl.Resources>
 
xmal에서 converter 사용    

<Control Style="{Binding Path=shape, Converter={StaticResource StyleConverter}, ConverterParameter=DogStyle}" Width="80" Height="80"/>
 
[ cs ]

    public class StyleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ResourceDictionary dic = Application.Current.Resources;

            Style returnStyle = new Style();

            switch (parameter.ToString())
            {
                case "DogStyle":
                    if ((String)value == "dog")
                    {
                        returnStyle = dic["dog1"] as Style;
                    }
                    else
                    {
                        returnStyle = dic["dog2"] as Style;
                    }
                    break;
                case "CatStyle":
                    switch ((String)value)
                    {
                        case "YellowCat":
                            returnStyle = dic["YCat"] as Style;
                            break;
                        case "RedCat":
                            returnStyle = dic["RCat"] as Style;
                            break;
                        default:
                            break;
                    }
                    break;
            }
            return returnStyle;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

 

 

 

 

Tags  Binding Converter, IValueConverter, ResourceDictio
  Relation Articles
[Languages-WPF] Binding Converter : IValueConverter (2013-08-06 12:16:38)
  Your Opinion
Member ID
150 letters
Copyright (C) SPATIUM. All rights reserved.
[SPATIUM]WebMaster Mail