我将ObservableCollection
绑定到DataGrid,我希望能够通过空行在observablecollection中添加项目。但是,当列表在运行时为空时,此空白行不会显示。我已经查找了这个问题,我有CanUserAddRows=true
,我有IsReadOnly=false
,我已经将datagrid的datacontext设置为observable集合,并且我有一个空的构造函数,它仍然无法正常工作。以下是我到目前为止的情况:
在XAML中:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource CostCenterListViewSource}}" Name="costCenterListDataGrid" VerticalAlignment CanUserAddRows="True" CanUserDeleteRows="True" IsReadOnly="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name, Mode=TwoWay}" Header="Cost Center" Width="102" />
<DataGridTextColumn x:Name="glCodeColumn" Binding="{Binding Path=GlCode, Mode=TwoWay}" Header="Gl Code" Width="80" />
<DataGridTextColumn x:Name="costCenterPercentageColumn" Binding="{Binding Path=CostCenterPercentage, Mode=TwoWay}" Header="%" Width="30" />
</DataGrid.Columns>
</DataGrid>
在我的代码背后,我有这个:
costCenterListDataGrid.DataContext = item.CostCenterList;
//when "item" was instantiated, it also created a new instance: costCenterList = new ObservableCollection<CostCenter>();
在CostCenter课程中我有:
public CostCenter()
{
}
public string Name
{
get { return name; }
set
{
if (String.IsNullOrEmpty(Name) || !Name.Equals(value))
{
name = value;
OnPropertyChanged("Name");
}
}
}
public string GlCode
{
get { return glCode; }
set
{
if (String.IsNullOrEmpty(GlCode) || !GlCode.Equals(value))
{
glCode = value;
OnPropertyChanged("GlCode");
}
}
}
public double CostCenterPercentage
{
get { return cPercentage; }
set
{
if (CostCenterPercentage != value)
{
cPercentage = value;
OnPropertyChanged("CostCenterPercentage");
}
}
}
编辑:这就是我在Window.Resources中的内容
<Window.Resources>
<CollectionViewSource x:Key="jobItemViewSource1" d:DesignSource="{d:DesignInstance my:JobItem, CreateList=True}" />
<CollectionViewSource x:Key="jobItemNotesHistoryViewSource" Source="{Binding Path=NotesHistory, Source={StaticResource jobItemViewSource1}}" />
<CollectionViewSource x:Key="CostCenterListViewSource" Source="{Binding Path=CostCenterList, Source={StaticResource jobItemViewSource1}}" />
<CollectionViewSource x:Key="CostCenterListAfesViewSource" Source="{Binding Path=Afes, Source={StaticResource CostCenterListViewSource}}" />
</Window.Resources>