2017-07-06 76 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? –