在ListView中显示和编辑各种项目类型

本文关键字:项目 类型 编辑 ListView 显示 | 更新日期: 2023-09-27 18:07:57

我在ListView中显示两种类型的项目:

abstract class Item
{
    string ID { get; }
}
class ItemTypeA : Item
{
    string Value { get; set; }
}
class ItemTypeB : Item
{
    string ValueOne { get; set; }
    string ValueTwo { get; set; }
}

(当然,还有更多的内容,它们使用各种接口,但为了示例起见,我省略了它,只显示了这些类型的"最终"结构)

基本上在ListView中,总是有一个ID列其他列取决于项目类型

我正在考虑在ListView.ViewGridView中使用某种动态列,其中底层视图模型将确定,应该显示哪些列(或给定项目类型的属性)。

同样适用于编辑-应该只有一个TextBox用于ItemTypeA和两个用于ItemTypeB编辑区域

所以基本上,我的问题是,如何解决这个问题,同时尽量避免为每个项目类型制作完整的单独视图(与ListView和编辑区)。

另外,我想避免直接在ListView中编辑项目,因为项目值偶尔会包含大量文本(甚至多行文本),这对用户来说很难编辑。

(我用的是Caliburn。

在ListView中显示和编辑各种项目类型

你可以为ListViewItem提供你自己的DataTemplateSelector:

public class MyTemplateSelector : DataTempolateSelector
{
     public override DataTemplate SelectTemplate(object item, DependencyObject container)
     {
        // here select temaplate, from dictionary for example, using item.GetType()
     }
}

你的Item视图模型类:

abstract class Item
{
    string ID { get; }
    MyTemplateSelector TemplateSelector { get; }
}

你的ListViewItem样式: