C# 空行在数据绑定的DataGrid中不可见
我有一个绑定到DataGrid的ObservableCollection,我希望能够在运行时使用实际的行在其中添加行。但是,空白行是不可见的,因此用户不能添加行。我已经对此进行了研究,添加了CanUserAddRows=true和IsReadOnly=false。我还为CostCenter对象添加了一个空白构造函数,但它仍然不起作用。以下代码是我目前掌握的代码: 在XAML中:C# 空行在数据绑定的DataGrid中不可见,c#,wpf,data-binding,datagrid,observablecollection,C#,Wpf,Data Binding,Datagrid,Observablecollection,我有一个绑定到DataGrid的ObservableCollection,我希望能够在运行时使用实际的行在其中添加行。但是,空白行是不可见的,因此用户不能添加行。我已经对此进行了研究,添加了CanUserAddRows=true和IsReadOnly=false。我还为CostCenter对象添加了一个空白构造函数,但它仍然不起作用。以下代码是我目前掌握的代码: 在XAML中: <Window.Resources> <CollectionViewSource x:Key
<Window.Resources>
<CollectionViewSource x:Key="jobItemViewSource1" d:DesignSource="{d:DesignInstance my:JobItem, CreateList=True}" />
<CollectionViewSource x:Key="CostCenterListViewSource" Source="{Binding Path=CostCenterList, Source={StaticResource jobItemViewSource1}}" />
</Window.Resources>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource CostCenterListViewSource}}" Name="costCenterListDataGrid" CanUserResizeRows="False" CanUserAddRows="True" CanUserDeleteRows="True" Background="LightGray" IsReadOnly="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name, Mode=TwoWay}" Header="Cost Center" />
<DataGridTextColumn x:Name="glCodeColumn" Binding="{Binding Path=GlCode, Mode=TwoWay}" Header="Gl Code"/>
<DataGridTextColumn x:Name="costCenterPercentageColumn" Binding="{Binding Path=CostCenterPercentage, Mode=TwoWay}" Header="%"/>
</DataGrid.Columns>
</DataGrid>
在JobItem类中:
private ObservableCollection<CostCenter> costCenterList;
public JobItem()
{
costCenterList = new ObservableCollection<CostCenter>();
}
public ObservableCollection<CostCenter> CostCenterList
{
get { return costCenterList; }
set
{
costCenterList = value;
}
}
private observeCollection成本中心列表;
公共工作项目(项目)
{
costCenterList=新的ObservableCollection();
}
公共可观测收集成本中心列表
{
获取{return costCenterList;}
设置
{
成本中心列表=价值;
}
}
在CostCenter类中,我有:
public CostCenter()
{
afes = new ObservableCollection<Afe>();
}
public CostCenter(ObservableCollection<Afe> afes, string glCode)
{
this.afes = afes;
this.glCode = glCode;
}
public string Name
{
get { return name; }
set
{
if (String.IsNullOrEmpty(Name) || !Name.Equals(value))
{
name = value;
OnPropertyChanged("Name");
}
}
}
public ObservableCollection<Afe> Afes
{
get { return afes; }
set { afes = value; }
}
public string GlCode
{
get { return glCode; }
set
{
if (String.IsNullOrEmpty(GlCode) || !GlCode.Equals(value))
{
glCode = value;
OnPropertyChanged("GlCode");
}
}
}
public double TotalPercentage
{
get { return totalPercentage; }
set
{
if (totalPercentage + value <= 100)
{
totalPercentage += value;
}
}
}
public double CostCenterPercentage
{
get { return cPercentage; }
set
{
if (CostCenterPercentage != value)
{
cPercentage = value;
OnPropertyChanged("CostCenterPercentage");
}
}
}
公共成本中心()
{
afes=新的可观测集合();
}
公共成本中心(ObservableCollection afes,字符串glCode)
{
this.afes=afes;
this.glCode=glCode;
}
公共字符串名
{
获取{返回名称;}
设置
{
if(String.IsNullOrEmpty(Name)| |!Name.Equals(value))
{
名称=值;
不动产变更(“名称”);
}
}
}
公共可观测集合Afes
{
获取{return afes;}
设置{afes=value;}
}
公共字符串GlCode
{
获取{return glCode;}
设置
{
if(String.IsNullOrEmpty(GlCode)| |!GlCode.Equals(value))
{
glCode=值;
OnPropertyChanged(“GlCode”);
}
}
}
公共双总百分比
{
获取{return totalPercentage;}
设置
{
如果(totalPercentage+value,这可能会被忽略,但在添加空白对象后是否会对dataGrid进行数据绑定
costCenterListDataGrid.DataContext = item.CostCenterList;
costCenterListDataGrid.DataBind()
这可能会被忽略,但在添加空白对象后是否会对dataGrid进行数据绑定
costCenterListDataGrid.DataContext = item.CostCenterList;
costCenterListDataGrid.DataBind()
您的CollectionViewSource
未创建IEditableCollectionView
类型集合视图,如果您希望数据网格显示添加、删除和可编辑行,则需要该集合视图
您的CollectionViewSource
没有创建IEditableCollectionView
类型的集合视图,如果您想让DataGrid
显示添加、删除和可编辑行,则需要该集合视图
错误1'System.Windows.Controls.DataGrid'不包含'DataBind'的定义,并且找不到接受'System.Windows.Controls.DataGrid'类型的第一个参数的扩展方法'DataBind'(是否缺少using指令或程序集引用?),我从未使用过.DataBind()以前。我认为这是第一行的目的。是的,很抱歉,我只是查看system.web而不是system.windows的databind控件。这不起作用。错误1'system.windows.Controls.DataGrid'不包含'databind'的定义,并且没有扩展方法'databind'接受'system.windows.Controls.DataGrid'类型的第一个参数'可以找到(您是否缺少using指令或程序集引用?)我以前从未使用过.DataBind()。我认为这就是第一行的目的。是的,很抱歉,我只是在查看system.web而不是system.windows的DataBind控件。这不起作用。我没有获得源={StaticResource jobItemViewSource1}这是VS在我使用数据源时自动创建的。它引用了我的Windows.ResourcesI不获取源={StaticResource jobItemViewSource1}这是VS在我使用数据源时自动创建的。它正在引用我的Windows.Resources我尝试使用:IEditableCollectionView ccView;ccView=costCenterListDataGrid.ItemsSource作为IEditableCollectionView;它始终以Null结束我尝试使用:IEditableCollectionView ccView;ccView=costCenterListDataGrid.ItemsSource作为IEditableCollectionView;并且它总是以null结尾