C# 具有动态可编辑列的DataGrid
我一直在尝试在WPF MVVM项目中创建一个带有动态列的可编辑的C# 具有动态可编辑列的DataGrid,c#,wpf,mvvm,dynamic,datagrid,C#,Wpf,Mvvm,Dynamic,Datagrid,我一直在尝试在WPF MVVM项目中创建一个带有动态列的可编辑的DataGrid。动态列的类型相同,即:decimal 目的是收集部门数量不确定的商店的部门总数。我试着在下面演示它 Day Dept1 Dept2 Dept3... TotalOfDepartments CashTotal CreditTotal ===================================================================== 1 100 200
DataGrid
。动态列的类型相同,即:decimal
目的是收集部门数量不确定的商店的部门总数。我试着在下面演示它
Day Dept1 Dept2 Dept3... TotalOfDepartments CashTotal CreditTotal
=====================================================================
1 100 200 50 350 50 300
2 75 100 0 175 25 150
所以,有许多商店的部门不明确,我的目标是收集每月
我想使部门、现金总额和信用总额列可编辑。我尝试过几种方法,比如:
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));
}
}
<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>
公共类每日新闻
{
public int ShopId{get;set;}
公共整数日{get;set;}
公共ObservableCollection部门列表{get;set;}
公共日报
{
this.DepartmentList=新的ObservableCollection();
}
}
公共课系
{
公共字符串名称{get;set;}
私人十进制总数;
公共十进制总数
{
获取{return total;}
设置{total=value;}
}
}
视图模型:
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));
}
}
<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>
公共类DataItemViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共DataItemViewModel()
{
this.MonthlyRevenues=新的可观察集合();
var d1=new dailrylevenues(){ShopId=1,Day=1};
d1.DepartmentList.Add(新部门(){Name=“department1”,总计=100});
d1.DepartmentList.Add(新部门(){Name=“department2”,总计=200});
var d2=new dailrylevenues(){ShopId=1,Day=2};
d2.DepartmentList.Add(新部门(){Name=“dePartment1”,总计=75});
d2.DepartmentList.Add(新部门(){Name=“dePartment2”,总计=150});
d2.DepartmentList.Add(新部门(){Name=“dePartment3”,总计=100});
本.月平均数.加上(d1);
本.月平均数.加上(d2);
}
每月晚上的私人可观察收集;
公众可观察的每月晚上收集
{
获取{return monthlyRevenues;}
设置
{
如果(月平均数!=值)
{
月平均数=价值;
物业变更(名称(月平均数));
}
}
}
私有void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
和XAML:
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));
}
}
<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,我想通过窗口/用户控件收集it部门的月度收入。每个商店一个月都有相同的部门数,但这并不意味着每个部门每天都应该有收入,它可以是零。该部门可能在任何一天关闭,因此当天不会增加任何收入。Shop2在同一个月可能有完全不同的部门,所以我不会在同一个屏幕上处理所有的商店
编辑1:添加了有关场景的更多信息。您可以采取多种不同的方法,每种方法都有优缺点。基于您对问题的更完整描述,我选择了自定义类型描述符方法 这里我们向daily revenues类添加一个自定义类型描述符
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;
}
}
公共类DailyLevenues:ICustomTypeDescriptor
{
public int ShopId{get;set;}
公共整数日{get;set;}
公共ObservableCollection部门列表{get;set;}
公共日报
{
this.DepartmentList=新的ObservableCollection();
}
公共部门十进制总数{get;}
公共十进制现金总额{get;}
公共十进制CreditTotal{get;}
公共属性集合GetAttributes()
{
返回新的AttributeCollection();
}
公共字符串GetClassName()
{
返回“每日晚上”;
}
公共字符串GetComponentName()
{
返回“”;
}
公共类型转换器GetConverter()
{
返回null;
}
公共事件描述符GetDefaultEvent()
{
返回null;
}
公共属性描述程序GetDefaultProperty()
{
返回null;
}
公共对象GetEditor(类型editorBaseType)
{
返回null;
}
公共事件描述符集合GetEvents()
{
返回null;
}
公共事件描述符集合GetEvents(属性[]属性)
{
返回null;
}
公共属性DescriptorCollection GetProperties()
{
PropertyDescriptorCollection pdc0=TypeDescriptor.GetProperties(typeof(DailyRevenues));
List pdList=新列表();
pdList.Add(pdc0[“日]);
对于(int i=0;i
风俗习惯