我一直在嘗試使用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欄目編輯。我有我喜歡嘗試幾種方法:
- populate dynamic datagrid column and binding with editable using mVVm
- Populating a DataGrid with Dynamic Columns in a Silverlight Application using MVVM
- How do I bind a WPF DataGrid to a variable number of columns?
這是我從最後的辦法最後一次嘗試。具體如下:
型號:
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:有關添加場景的更多信息。
你見過/考慮過綁定DataTable嗎?例如:https://stackoverflow.com/a/44206066/1506454 – ASh
@ASh,我怎樣才能使它雙向。我的意思是編輯是好的,但我怎麼才能獲得viewmodel的數據來做一些魔術呢? –
什麼應該是雙向的?用DataTable綁定單擊DataGrid單元格應該允許編輯值。該cahnge將反映在dataTable單元格 – ASh