Binding.Converter 속성

정의

원본과 대상 간에 전달될 때 데이터를 수정하기 위해 바인딩 엔진에서 호출하는 변환기 개체를 가져오거나 설정합니다.

public:
 property IValueConverter ^ Converter { IValueConverter ^ get(); void set(IValueConverter ^ value); };
IValueConverter Converter();

void Converter(IValueConverter value);
public IValueConverter Converter { get; set; }
var iValueConverter = binding.converter;
binding.converter = iValueConverter;
Public Property Converter As IValueConverter
<Binding Converter="converterReference"/>

속성 값

데이터를 수정하는 IValueConverter 개체입니다.

예제

바인딩에서 변환기를 사용하려면 먼저 변환기 클래스의 instance 만듭니다. 다음 예제에서는 이를 XAML 파일의 리소스로 보여줍니다.

<UserControl.Resources>
  <local:DateToStringConverter x:Key="Converter1"/>
</UserControl.Resources>
<TextBlock Grid.Column="0" Margin="5,0"
  Text="{Binding Month, Converter={StaticResource Converter1}}"/>
// Custom class implements the IValueConverter interface.
public class DateToStringConverter : IValueConverter
{

    #region IValueConverter Members

    // Define the Convert method to change a DateTime object to 
    // a month string.
    public object Convert(object value, Type targetType, 
        object parameter, string language)
    {
        // The value parameter is the data from the source object.
        DateTime thisdate = (DateTime)value;
        int monthnum = thisdate.Month;
        string month;
        switch (monthnum)
        {
            case 1:
                month = "January";
                break;
            case 2:
                month = "February";
                break;
            default:
                month = "Month not found";
                break;
        }

        // Return the month value to pass to the target.
        return month;
    }

    // ConvertBack is not implemented for a OneWay binding.
    public object ConvertBack(object value, Type targetType, 
        object parameter, string language)
    {
        throw new NotImplementedException();
    }

    #endregion
}
' Custom class implements the IValueConverter interface.
Public Class DateToStringConverter
    Implements IValueConverter

    ' Define the Convert method to change a DateTime object to
    ' a month string.
    Public Function Convert(ByVal value As Object, _
        ByVal targetType As Type, ByVal parameter As Object, _
        ByVal language As System.String) As Object _
        Implements IValueConverter.Convert

        ' value is the data from the source object.
        Dim thisdate As DateTime = CType(value, DateTime)
        Dim monthnum As Integer = thisdate.Month
        Dim month As String
        Select Case (monthnum)
            Case 1
                month = "January"
            Case 2
                month = "February"
            Case Else
                month = "Month not found"
        End Select
        ' Return the value to pass to the target.
        Return month

    End Function

    ' ConvertBack is not implemented for a OneWay binding.
    Public Function ConvertBack(ByVal value As Object, _
        ByVal targetType As Type, ByVal parameter As Object, _
        ByVal language As System.String) As Object _
        Implements IValueConverter.ConvertBack
        Throw New NotImplementedException

    End Function
End Class

설명

IValueConverter 인터페이스를 구현하고 Convert 메서드를 구현하여 변환기를 만듭니다. 해당 메서드는 바인딩이 대상으로 하는 종속성 속성과 동일한 형식의 개체 또는 최소한 암시적으로 강제 변환되거나 대상 형식으로 변환될 수 있는 형식을 반환해야 합니다.

적용 대상

추가 정보