空数行在数据绑定的DataGrid中不可见

时间:2012-06-26 17:52:28

标签: c# wpf data-binding datagrid observablecollection

我有一个绑定到DataGrid的ObservableCollection,我希望能够在运行时使用实际行在其中添加行。但是,空行不可见,因此用户无法添加行。我已经研究过了,我已经添加了CanUserAddRows = true和IsReadOnly = false。我还为CostCenter对象添加了一个空白构造函数,它仍然无法正常工作。下面的代码是我到目前为止的代码:

在XAML中:

<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>

在后面的代码中我设置了datacontext:

costCenterListDataGrid.DataContext = item.CostCenterList;

在JobItem类中:

private ObservableCollection<CostCenter> costCenterList;
public JobItem()
{
    costCenterList = new ObservableCollection<CostCenter>();         
}

public ObservableCollection<CostCenter> CostCenterList
{
    get { return costCenterList; }
    set
    {
        costCenterList = value;
    }

}

在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");
            }
        }

    }

我试图省略不相关的代码,OnPropertyChanged方法正在运行,因为我在其他代码中使用它,否则,希望这是足够的代码,以帮助我解决问题。 JobItem的datacontext也正常工作,我还没有在这个片段中添加。

2 个答案:

答案 0 :(得分:0)

这可能会被忽略,但是在添加空白对象后是否对dataGrid进行了数据绑定?

costCenterListDataGrid.DataContext = item.CostCenterList;
costCenterListDataGrid.DataBind()

答案 1 :(得分:0)

如果您希望CollectionViewSource显示添加,删除和可修改的行,则IEditableCollectionView未创建DataGrid类型集合视图。

http://blogs.msdn.com/b/vinsibal/archive/2008/10/01/overview-of-the-editing-features-in-the-wpf-datagrid.aspx