【问题标题】:DataGrid with Dynamic Editable Columns具有动态可编辑列的 DataGrid
【发布时间】:2017-12-11 00:08:37
【问题描述】:

我一直在尝试在 WPF MVVM 项目中使用动态列创建可编辑的DataGrid。动态列将是同一类型,即:decimal

目的是收集部门数量不定的商店的部门总数。我试图在下面演示它。

Day Dept1   Dept2   Dept3... TotalOfDepartments CashTotal CreditTotal
=====================================================================
1    100     200     50            350             50       300
2     75     100      0            175             25       150  

所以,有很多店铺不定部门,我的目标是收集月份

我想让 Department、CashTotal 和 CreditTotal 列可编辑。我尝试过几种方法:

这是我最后一次尝试的最后一种方法。如下:

型号:

 public class DailyRevenues
    {
        public int ShopId { get; set; }
        public int Day { get; set; }
        public ObservableCollection<Department> DepartmentList { get; set; }

        public DailyRevenues()
        {
            this.DepartmentList = new ObservableCollection<Department>();
        }
    }

    public class Department
    {
        public string Name { get; set; }

        private decimal total;
        public decimal Total
        {
            get { return total; }
            set { total = value; }
        }
    }

视图模型:

public class DataItemViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public DataItemViewModel()
        {
            this.MonthlyRevenues = new ObservableCollection<DailyRevenues>();

            var d1 = new DailyRevenues() { ShopId = 1, Day = 1 };
            d1.DepartmentList.Add(new Department() { Name = "Deapartment1", Total = 100 });
            d1.DepartmentList.Add(new Department() { Name = "Deapartment2", Total = 200 });

            var d2 = new DailyRevenues() { ShopId = 1, Day = 2 };
            d2.DepartmentList.Add(new Department() { Name = "Deapartment1", Total = 75 });
            d2.DepartmentList.Add(new Department() { Name = "Deapartment2", Total = 150 });
            d2.DepartmentList.Add(new Department() { Name = "Deapartment3", Total = 100 });

            this.MonthlyRevenues.Add(d1);
            this.MonthlyRevenues.Add(d2);
        }

        private ObservableCollection<DailyRevenues> monthlyRevenues;
        public ObservableCollection<DailyRevenues> MonthlyRevenues
        {
            get { return monthlyRevenues; }
            set
            {
                if (monthlyRevenues != value)
                {
                    monthlyRevenues = value;
                    OnPropertyChanged(nameof(MonthlyRevenues));
                }
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

还有 XAML:

<DataGrid ItemsSource="{Binding MonthlyRevenues}" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Day" Binding="{Binding Path=Day}" />
            <DataGridTextColumn Header="{Binding Path=MonthlyRevenues[0].DepartmentList[0].Name}" Binding="{Binding Path=DepartmentList[0].Total, Mode=TwoWay}" />
            <DataGridTextColumn Header="{Binding Path=DepartmentList[1].Name}" Binding="{Binding Path=DepartmentList[1].Total, Mode=TwoWay}" />
            <DataGridTextColumn Header="Department Total"/>
            <DataGridTextColumn Header="Cash Total" />
            <DataGridTextColumn Header="Credit Total" />
        </DataGrid.Columns>
    </DataGrid>

不幸的是,在最后一次尝试中,在 XAML 上使用索引器并不能帮助我处理动态列,而且我找不到以任何其他方式绑定它们的方法。

更多信息: 上面的数据网格(和数据演示)属于 shop1,我想在窗口/用户控件上收集其部门的月收入。每个商店在一个月内的部门数量相同,但这并不意味着每个部门每天都应该有收入,它可以为零。该部门可能会在任何一天关闭,因此当天不会增加任何收入。 Shop2 可能在同一月份有完全不同的部门,所以我不会在同一个屏幕上处理所有商店。

编辑 1:添加了有关场景的更多信息。

【问题讨论】:

  • 您见过/考虑过绑定 DataTable 吗?例如:stackoverflow.com/a/44206066/1506454
  • @ASh,我怎样才能做到双向。我的意思是编辑是可以的,但是我如何让 viewmodel 上的数据在那里发挥作用呢?
  • 什么应该是双向的?使用 DataTable 绑定单击 DataGrid 单元格应该允许编辑值。更改将反映在 dataTable 单元格中
  • DataTable 绑定单击带有 mvvm 的 DataGrid 单元格?我会调查的。
  • DataTable 不会帮助您处理可变数量的列。您对行/列结构提出了很多要求。有一种方法可以做你所要求的,但我需要确切地了解你在做什么。在您的示例中,第 1 天商店 1 有 2 个部门。在第 2 天它有 3 个,其中 2 个与第 1 天的名称相同。如果第 2 天的所有名称都不同怎么办?那么有5列吗?如果商店 2 有部门。同名是同一列吗?以此类推。

标签: c# wpf mvvm dynamic datagrid


【解决方案1】:

您可以采取多种不同的方法,每种方法都有优缺点。根据您对问题的更完整描述,我选择了自定义类型描述符方法。

在这里,我们将自定义类型描述符添加到每日收入类...

public class DailyRevenues : ICustomTypeDescriptor
{
    public int ShopId { get; set; }
    public int Day { get; set; }
    public ObservableCollection<Department> DepartmentList { get; set; }

    public DailyRevenues()
    {
        this.DepartmentList = new ObservableCollection<Department>();
    }
    public decimal TotalOfDepartments { get;  }
    public decimal CashTotal { get;  }
    public decimal CreditTotal { get; }

    public AttributeCollection GetAttributes()
    {
        return new AttributeCollection();
    }

    public string GetClassName()
    {
        return "DailyRevenues";
    }

    public string GetComponentName()
    {
        return "";
    }

    public TypeConverter GetConverter()
    {
        return null;
    }

    public EventDescriptor GetDefaultEvent()
    {
        return null;
    }

    public PropertyDescriptor GetDefaultProperty()
    {
        return null;
    }

    public object GetEditor(Type editorBaseType)
    {
        return null;
    }

    public EventDescriptorCollection GetEvents()
    {
        return null;
    }

    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        return null;
    }

    public PropertyDescriptorCollection GetProperties()
    {
        PropertyDescriptorCollection pdc0 = TypeDescriptor.GetProperties(typeof(DailyRevenues));
        List<PropertyDescriptor> pdList = new List<PropertyDescriptor>();
        pdList.Add(pdc0["Day"]);
        for (int i = 0; i < DepartmentList.Count; ++i)
        {
            pdList.Add(new DailyRevenuesProperty(DepartmentList[i].Name, i));
        }
        pdList.Add(pdc0["TotalOfDepartments"]);
        pdList.Add(pdc0["CashTotal"]);
        pdList.Add(pdc0["CreditTotal"]);
        return new PropertyDescriptorCollection(pdList.ToArray());
    }

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        return GetProperties();
    }

    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }
}

自定义类型描述符允许我们“扁平化”数据结构。随着部门数量的变化,对象上的属性数量也会发生变化。这需要每日收入类的自定义属性描述符...

public class DailyRevenuesProperty : PropertyDescriptor
{
    int _index;
    public DailyRevenuesProperty(string name, int index)
        : base(name, new Attribute[0])
    {
        _index = index;
    }
    public override Type ComponentType
    {
        get
        {
            return typeof(DailyRevenues);
        }
    }

    public override bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public override Type PropertyType
    {
        get
        {
            return typeof(decimal);
        }
    }

    public override bool CanResetValue(object component)
    {
        return false;
    }

    public override object GetValue(object component)
    {
        DailyRevenues dr = component as DailyRevenues;
        if(dr != null && _index >= 0 && _index < dr.DepartmentList.Count)
        {
            return dr.DepartmentList[_index].Total;
        }
        else
        {
            return (decimal)0;
        }
    }

    public override void ResetValue(object component)
    {
    }

    public override void SetValue(object component, object value)
    {
        DailyRevenues dr = component as DailyRevenues;
        if (dr != null && _index >= 0 && _index < dr.DepartmentList.Count && value is decimal)
        {
            dr.DepartmentList[_index].Total = (decimal)value;
        }
    }

    public override bool ShouldSerializeValue(object component)
    {
        return false;
    }
}

现在我们需要一个类型化的列表。这将替换 observable 集合。

public class MonthlyRevenues : ObservableCollection<DailyRevenues>, ITypedList
{
    public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
    {
        if(Count > 0)
        {
            return TypeDescriptor.GetProperties(this[0]);
        }
        else
        {
            return TypeDescriptor.GetProperties(typeof(DailyRevenues));
        }
    }

    public string GetListName(PropertyDescriptor[] listAccessors)
    {
        return "Monthly Revenues";
    }
}

当自动生成列时,数据网格会检查项目集合是否为类型列表。如果是,则数据网格会查询类型列表上的属性。

最后总结一下,这里是数据网格...

    <DataGrid ItemsSource="{Binding MonthlyRevenues}" AutoGenerateColumns="true" />

这是生成的网格...

这种方法有很多限制。首先,我依靠数据网格来自动生成列。如果我想在标题文本中添加空格等内容,我需要做更多的事情。其次,我指望部门名称是有效的财产名称,并且不会与日常收入类中的其他财产发生冲突。如果没有,那么我将需要做更多的事情。以此类推。

【讨论】:

  • 感谢您的帮助。我研究了你的方法,我想我仍然无法以我应该的方式解释我的问题。我已根据您的更改通过声明MonthlyRevenues 属性而不是ObservableCollection 对视图模型进行了更改。我知道DailyRevenues : ICustomTypeDescriptor 类属性的数据网格可编辑性取决于设置器。 DepartmentList 有一个设置器,但生成的网格在部门 1 和 2 的列上不可编辑。我需要另一个 类型列表 还是另一个 自定义类型描述符
  • 没有。这很容易解决。我没有意识到您希望部门总数可编辑,因为它没有设置器。我已经编辑了答案,因此您现在可以编辑部门总数。更改(除了将 setter 添加到部门总数)是属性描述符:(1)将 IsReadOnly 更改为 false 和(2)实现 SetValue 函数。
  • 这看起来很有希望。但仍有一些我无法弄清楚。如何在 Department Total 更改时触发 propertyChanged,即将 Department1 从 100 更改为 200,以便我可以再次计算 TotalOfDepartments?
  • 非常简单。在 Department 上实现 INotifyPropertyChanged 接口,并监听 DailyRevenues 中 Total 属性的变化。
  • 谢谢,AQuirky 花了一些时间,但您的回答让我找到了解决方案。我会尽快编辑问题并发布我的方法。
猜你喜欢
  • 2013-02-23
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多