设置AutoGenerateColumns = true并按DataType自动转换 [英] Set AutoGenerateColumns=true and auto convert by the DataType

查看:79
本文介绍了设置AutoGenerateColumns = true并按DataType自动转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的ObservableCollection,其中有40多个不同类型的列. 一些列是列表. 由于列的数量不同,我不想设置AutoGenerateColumns = false,但是datagrid以数据类型的字符串形式显示此信息:System.Collection.Generic.List'1 [System.double] 我想显示一个字符串,如"10,2,30,5.2" 我是否可以创建一个转换器来检查值的类型,并在返回列表的情况下返回类似的字符串? 我如何从Xaml读取它(我使用mvvm patteren) 谢谢!

I have a big ObservableCollection with 40+ columns that havn't the same type. Some of the columns are lists. I don't want to set AutoGenerateColumns=false because of the amont of the columns but the datagrid presents this in string of the datatype: System.Collection.Generic.List'1[System.double] and I want to present a string of the value like "10,2,30,5.2" Can I create a converter that check the typeof the value and return a string like this if it's a list? how can I read it from the Xaml (I use mvvm patteren) thanks!

推荐答案

一个选项可能是将数据类型从List更改为您自己的从List继承的类,并重写ToString方法以返回所需的字符串.像这样:

An option could be changing the datatype from List to your own class inheriting from List and overriding the ToString method to return the string you want. Something like this:

public class MyDoubleList : List<double>
{
    public override string ToString()
    {
        string s = "";
        foreach (double d in this)
        {
            s = s + d.ToString() + ";";
        }
        return s;
    }
}

用法样本:

MyDoubleList l = new MyDoubleList();
l.Add(12);
l.Add(25);

string a = l.ToString();

这篇关于设置AutoGenerateColumns = true并按DataType自动转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆