2

我将 an 绑定ObservableCollection到 DataGrid,并且我希望能够通过空白行在 observablecollection 中添加项目。但是,当列表在运行时为空时,此空白行不会显示。我已经查找了这个问题,我有CanUserAddRows=true,我有IsReadOnly=false,并且我已经将数据网格的 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>
4

0 回答 0