2017-07-06 77 views
4

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

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

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

因此,有许多商店不确定部门和我的目标是收集一个月

我要让部,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上使用索引器并不能帮助我处理动态列,而且我也找不到一种方法来以任何其他方式绑定它们。

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

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

+0

你见过/考虑过绑定DataTable吗?例如:https://stackoverflow.com/a/44206066/1506454 – ASh

+0

@ASh,我怎样才能使它双向。我的意思是编辑是好的,但我怎么才能获得viewmodel的数据来做一些魔术呢? –

+1

什么应该是双向的?用DataTable绑定单击DataGrid单元格应该允许编辑值。该cahnge将反映在dataTable单元格 – ASh

回答

3

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

在这里,我们添加一个自定义类型说明符日常收入类...

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; 
    } 
} 

现在我们需要一个打字清单。这取代了可观察的集合。

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" /> 

这是造成电网...

enter image description here

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

+0

我感谢你的帮助。我研究了你的方法,我想我仍然不能以我应该的方式解释我的问题。 我通过声明'MonthlyRevenues'属性而不是'ObservableCollection'来根据您的更改对视图模型进行更改。我知道'DailyRevenues:ICustomTypeDescriptor'类属性的datagrid可编辑性取决于setter。 'DepartmentList'有一个setter,但是生成的网格在Department 1&2的列上不可编辑。我是否需要另一个_typed list_或另一个_Custom Type Descriptor_? –

+1

不,这很容易修复。我没有意识到你想让部门总数可编辑,因为没有二传手。我编辑了答案,以便现在可以编辑部门总计。除了将setter添加到部门总计之外,这些更改是属性描述符:(1)将IsReadOnly更改为false,并(2)实现SetValue函数。 – AQuirky

+0

这看起来很有希望。但还有一些我不明白的地方。我怎样才能在部门总数的变化中触发propertyChanged,即将Department1从100更改为200,以便我可以再次计算TotalOfDepartments? –