【问题标题】:How to display a list in a column of a ListView (WPF and C#)如何在 ListView 的列中显示列表(WPF 和 C#)
【发布时间】:2014-12-21 16:52:36
【问题描述】:

我有一个绑定到 ListView 的类

class Estudante
{
    public string primeironome { get; set; }
    public string segundonome { get; set; }
    public int[] notas { get; set; }
}

主窗口

ObservableCollection<Estudante> arrList = new ObservableCollection<Estudante>();
ListaAlunos.ItemsSource = arrList;

XAML

DisplayMemberBinding="{Binding primeironome}"
DisplayMemberBinding="{Binding segundonome}"
DisplayMemberBinding="{Binding notas}"

但是当我向集合中添加任何数据时,例如:

arrList.Add(
            new Estudante
            {
                primeironome = "Svetlana",
                segundonome = "Omelchenko",
                notas = new int[] { 98, 92, 81, 60 }
            });

ListView 上的结果是:

C1        C2         C3
Svetlana  Omelchenko Int32[] Array

我希望它是:

 C1        C2         C3
 Svetlana  Omelchenko 98, 92, 81, 60

谁能帮帮我?

【问题讨论】:

    标签: c# wpf listview binding observablecollection


    【解决方案1】:

    新建string属性,例如

    class Estudante
    {
        public string primeironome { get; set; }
        public string segundonome { get; set; }
        public int[] notas { get; set; }
        public string StringNotas
        {
           get 
             {
                return string.Join(",", notas);
             }
       }
    }
    

    并绑定到StringNotas

    Xaml

    DisplayMemberBinding="{Binding primeironome}"
    DisplayMemberBinding="{Binding segundonome}"
    DisplayMemberBinding="{Binding StringNotas}"
    

    您仍然会以相同的方式创建数据

    arrList.Add(
                new Estudante
                {
                    primeironome = "Svetlana",
                    segundonome = "Omelchenko",
                    notas = new int[] { 98, 92, 81, 60 }
                });
    

    你将在MainWindow中以同样的方式绑定它

    ObservableCollection<Estudante> arrList = new ObservableCollection<Estudante>();
    ListaAlunos.ItemsSource = arrList;
    

    【讨论】:

    • 感谢您的快速响应!效果很好!
    • 当然,np,很高兴我能帮上忙 :)
    猜你喜欢
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多