惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
C
Check Point Blog
B
Blog RSS Feed
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
小众软件
小众软件
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
GbyAI
GbyAI
The Cloudflare Blog
博客园 - 叶小钗
S
SegmentFault 最新的问题
博客园 - Franky
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
B
Blog
Jina AI
Jina AI

博客园 - 极无宪

小红书如何采集市集 Textrimming 失效怎么办? 现在找个vs2019 安装好难呀! WPF:如何将调试信息写入到输出窗口? System.Net.WebException: 远程服务器返回错误: (405) 不允许的方法。 警惕:开启了 SQL Profiler监测SQL语句忘记关了,C盘居然被占满了 nuget 总是获取失败怎么办? 还在用WebBrowser吗?你out了! 关于打印机共享的注意事项——又被叫去修电脑了 ServicesController 的使用 winform中坐标系转换的问题,获取某点在屏幕中的绝对位置等 使用本地IIS Web 服务起转到win7 iis7后的系列问题 客户端元素中找不到与此名称匹配的终结点 关于错误的友好提示 调侃 亚马逊的 kindlegen propertygrid 下拉列表 VC编写在windows7下以管理员权限运行的程序(转) vs 已经在解决方案中打开了具有该名称的项目 解决方案"System.InvalidOperationException: 配置有 NoSecurityChanges 标志的 AppDomainManager 修改了 AppDomain 的安全状态"
MVVM转换器Int2StringConverter基础类
极无宪 · 2016-10-21 · via 博客园 - 极无宪

2016-10-21 17:22  极无宪  阅读(417)  评论()    收藏  举报

很多时候都是会用到int 转换为字符串的情况的转换器

可能在编辑界面需要一个下拉框来显示列表

在列表页面需要一个转换器显示为具体的字符串数据

这个时候使用,有一个Items属性的转换器,就都搞定了

using Client.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace Client.Common.Converter
{
    [ValueConversion(typeof(int), typeof(string))]
    public class Int2StringConverter : NotifyPropertyChanged, IValueConverter
    {
        public Int2StringConverter()
        {
            Items = new ObservableCollection<ListItem>();
            InitialItems();
        }

        protected virtual void InitialItems()
        {

        }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = string.Empty;
            if (value == null)
            {
                return result;
            }
            int sourceValue = System.Convert.ToInt32(value);
            var model = Items.Where(f => f.Value == sourceValue).FirstOrDefault();
            if (model != null)
                result = model.DisplayName;
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int result = -1;
            if (value == null)
            {
                return result;
            }
            string sourceValue = value.ToString();
            var model = Items.Where(f => f.DisplayName == sourceValue);
            if (model.Count() > 0)
                result = model.First().Value.Value;
            return result;
        }


        private ObservableCollection<ListItem> items;

        public ObservableCollection<ListItem> Items
        {
            get { return items; }
            set
            {
                items = value;
                OnPropertyChanged("Items");
            }
        }


    }
}