标题为"*"的DataGridColumn已存在于DataGrid的Columns集合中 [英] DataGridColumn with Header '*' already exists in the Columns collection of a DataGrid

查看:46
本文介绍了标题为"*"的DataGridColumn已存在于DataGrid的Columns集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有MVVM模式的WPF应用程序.在我看来,我必须绑定一个 ObservableCollection 进行查看.在这种视图下,我有一个 ListBox 和一个 DataGrid 都绑定到相同的 ObservableCollection ,但是它们执行不同的操作,例如事件,样式等.

I have a WPF application with MVVM pattern. In one of my view, I have to bind an ObservableCollection to view. In that view, I have one ListBox and one DataGrid both bind to the same ObservableCollection but doing different things like events, style etc..

我一次只需要显示这些控件之一,然后创建两个用户控件,一个用于 DataGrid ,另一个用于 ListBox .然后,我通过在主视图上放置 ContentControl 在它们之间进行切换(类似于此

I need only one of these controls displayed at a time and what I did is created two user controls, one for DataGrid and other for ListBox. And I switched between them by placing a ContentControl on the main view(something similar to this blog. The default view is DataGrid and when click on a button the other view is displayed(i.e. ListBox). Up to this are working fine.

要记住的另一件事是,通过使用以下

One more thing to keep in mind that the Data Grid columns are generated dynamically by using the solution described in the following link. So when I go back to DataGrid view it's throwing an error while adding columns to Data Grid in foreach statement (pls refer the answer of the previous link) like

" DataGrid 的Columns集合中已经存在标题为'Ord'的DataGridColumn.DataGrid无法共享列,并且不能包含重复的列实例."

"DataGridColumn with Header 'Ord' already exists in the Columns collection of a DataGrid. DataGrids cannot share columns and cannot contain duplicate column instances."

但是我确定在将列添加到 DataGrid 之前,其 Count 属性为零(dataGrid.Columns.Count()).那么 DataGrid 标头属性如何持久保存?有什么方法可以清除标头值?

But I'm sure that before adding columns to DataGrid its Count property is zero(dataGrid.Columns.Count()). So how the DataGrid header properties are persisted? Is there any way to clear the header values?.

请建议...

推荐答案

在使用

I had have the same error after using the behavior in the mentioned link. The question is old but in case someone else has the same problem, I solved it by adding a 'bridge' class to use instead of adding the columns directly.

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace AlyElHaddad.Stackoverflow
{
    public class DataGridColumnCollection : ObservableCollection<DataGridColumn>
    {
        public DataGridColumnCollection()
            : base()
        { }
        public DataGridColumnCollection(IEnumerable<DataGridColumn> collection)
            : base(collection)
        { }
        public DataGridColumnCollection(List<DataGridColumn> list)
            : base(list)
        { }
    }
}

在XAML中,不是直接添加列,而是将它们添加到 DataGridColumnCollection 内.

In XAML, instead of adding the columns directly, add them inside a DataGridColumnCollection.

<aly:DataGridColumnCollection xmlns:aly="clr-namespace:AlyElHaddad.Stackoverflow">
    <DataGridTextColumn Header="Column1" Binding="{Binding Column1}"/>
    <DataGridTextColumn Header="Column2" Binding="{Binding Column2}"/>
    <DataGridTextColumn Header="Column3" Binding="{Binding Column3}"/>
</aly:DataGridColumnCollection>

这篇关于标题为"*"的DataGridColumn已存在于DataGrid的Columns集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆