1

我有一个绑定到 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>

在后面的代码中,我设置了数据上下文:

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 的数据上下文也在工作,我也没有在这个片段中添加它。

4

2 回答 2

0

如果您希望显示添加、删除和可编辑行,则您CollectionViewSource没有创建所需的类型集合视图。IEditableCollectionViewDataGrid

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

于 2012-06-27T08:14:53.497 回答
0

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

costCenterListDataGrid.DataContext = item.CostCenterList;
costCenterListDataGrid.DataBind()
于 2012-06-26T18:01:12.470 回答