接口转换 数据库列表的内容 显示在datagrid

本文介绍了如何在WPF中使用自定义值转换器(IValueConverter)进行数据绑定的转换,通过两个具体示例,一个是地址转换器(AddressConverter),另一个是车型转换器(CarTypeConverter),展示了从复杂数据类型到简单显示文本的转换过程。
public class AddressConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string result = string.Empty;

            if (value != null)
            {
                string[] CustomCode = value.ToString().Split(',');

                for (int i = 0; i < CustomCode.Count(); i++)
                {
                    var tempCustom = CodeListHelper.AllAddressList.Where(c => c.SelectValue == CustomCode[i]).FirstOrDefault();

                    if (i == 0)
                    {
                        if (tempCustom == null)
                        {
                            result = CustomCode[i];
                        }
                        else
                        {
                            result = tempCustom.DisplayValue;
                        }
                    }
                    else
                    {
                        if (tempCustom == null)
                        {
                            result += "," + CustomCode[i];
                        }
                        else
                        {
                            result += "," + tempCustom.DisplayValue;
                        }
                    }
                }

                return result;

            }

            return result;
        }

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

上面是转换活的例子,下面是转换死的例子

 public class CarTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var tempCustom = CodeListHelper.DispatchTypeList.Where(c => c.SelectValue == value.ToString()).FirstOrDefault();

                if (tempCustom != null)
                {
                    return tempCustom.DisplayValue;
                }
            }

            return value;
        }

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

说这个意思。

转载于:https://www.cnblogs.com/Early-Bird/p/4269196.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值