mvvm里面的转换器的使用

有的时候,数据库存的是0,1,3,但是页面显示的时候0:未读,已读。。。

1.定义转换器

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace Glodon.Module.DocRegisterManage.Help
{

    public class StateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            if (int.Parse(value.ToString()) == 1)
            {
                return "A";
            }
            else if (int.Parse(value.ToString()) == 2)
            {
                return "B";
            }
            else
            {
                return "C";
            }
        }

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


}

2.在XMAL文件引入转换器命名空间

  xmlns:helper="clr-namespace:Glodon.Module.DocRegisterManage.Help"

 

3.声明资源

<UserControl.Resources>
<helper:StateConverter x:Key="StateConvert"/>
<helper:DataContextProxy x:Key="DataContextProxy" />

 

4.在gv里面使用资源

<TR:GridViewDataColumn IsReadOnly="True" DataMemberBinding="{Binding DState,Converter={StaticResource StateConvert}, Mode=OneWay}" Header="状态" IsFilterable="False" Width="80"   HeaderTextAlignment="Center"  />

 

转载于:https://www.cnblogs.com/muer/archive/2012/04/10/MVVM_Converter.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值