C# – Converter

By | 2023년 7월 20일
Table of Contents

C# – Converter

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

IValueConverter 를 구현해서 컨버터를 생성할 수 있다.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Data;

namespace Test.Helpers
{
    public class DeviceStatusStrConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((string) value);
        }

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

생성된 컨버터를 xaml 에서 사용할 수 있다.


xmlns:conv="clr-namespace:Test.Helpers"

    <UserControl.Resources>
        <conv:DeviceStatusStrConverter x:Key="Converter"/>
    </UserControl.Resources>

<DataGridTextColumn Binding="{Binding DeviceStatusStr, Converter={StaticResource Converter}}" />

답글 남기기