【发布时间】: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 列可编辑。我尝试过几种方法:
- 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 上使用索引器并不能帮助我处理动态列,而且我找不到以任何其他方式绑定它们的方法。
更多信息: 上面的数据网格(和数据演示)属于 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