【问题标题】:Blank Row is not visible in data-bound DataGrid空白行在数据绑定 DataGrid 中不可见
【发布时间】:2012-06-26 17:52:28
【问题描述】:

我有一个绑定到 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 的数据上下文也可以工作,我也没有在这个 sn-p 中添加。

【问题讨论】:

  • 我没有得到 Source={StaticResource jobItemViewSource1}}
  • 这是我使用数据源时由VS自动创建的。它引用了我的 Windows.Resources

标签: c# wpf data-binding datagrid observablecollection


【解决方案1】:

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

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

【讨论】:

  • 错误 1“System.Windows.Controls.DataGrid”不包含“DataBind”的定义,并且没有扩展方法“DataBind”接受“System.Windows.Controls.DataGrid”类型的第一个参数可以找到(您是否缺少 using 指令或程序集引用?)
  • 我以前从未使用过 .DataBind()。我认为这就是第一行的用途。
  • 是的,对不起,我只是在查看 system.web 的数据绑定控件而不是 system.windows 这不起作用。
【解决方案2】:

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

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

【讨论】:

  • 我尝试使用:IEditableCollectionView ccView; ccView = costCenterListDataGrid.ItemsSource as IEditableCollectionView;它总是以 null 结束
猜你喜欢
  • 2012-09-06
  • 1970-01-01
  • 2012-08-29
  • 2018-07-15
  • 2011-12-04
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 2014-10-14
相关资源
最近更新 更多