在TabController + DataGrid中使用反序列化的XML数据

时间:2011-08-08 03:09:06

标签: c# wpf datagrid tabcontrol

在此之后:Acess to a DataGrid through C# code and manipulate data inside the DataGrid我决定我应该反序列化我的XML数据并以这种方式使用它,因为我需要在我的应用程序中进行基本的CRUD操作。

我已经有了我的xml数据类(使用XSD工具,你可以在这里找到这个类 - > http://pastebin.com/6GWFAem6)并对我的数据进行反序列化,问题是:

  1. 我需要一个TabControl,其中包含与我的xml中的Semestre一样多的选项卡,每个选项卡都有GPASemestre.Nome标题。

  2. 在每个标签内,我需要一个带有特定Semestre的Cadeiras的DataGrid。

  3. 我需要能够在DataGrid和标签中输入CRUD数据。
  4. 问题:

    1. 要解决所有这些问题,您认为最好的是什么?创建所有内容(tabs + datagrid)并进行必要的绑定(我真的不知道它们将会是什么)/以某种方式填充DataGrid,仅在C#中?或者有一种方法可以使用XAML简化C#中的代码?
    2. Cadeiras存储在数组中,所以每次我添加一个新数组时,我需要创建一个新数组(或者创建一个包含更多空格的新数组并管理它),我已经在这里看到了一些问题,其中ppl使用了List但是哪有麻烦,是否可以使用清单?如果是这样,我必须在XSD自动生成的类中更改什么?
    3. 提前致谢!

1 个答案:

答案 0 :(得分:0)

我建议尽可能使用data-bindingdata-templating(如果你还没有这样做的话,请阅读那些),因为这样可以很好地运行自动生成的类需要调整以支持它。

第一步是在所有非集合的属性中实现INotifyPropertyChanged,这样如果更改属性,UI将自动更新。最好只使用数组进行反序列化,然后将元素复制到类型为ObservableCollection<T>的属性,或者实现INotifyCollectionChanged的任何其他集合,以便在将新项添加到收集,永远不要再触摸阵列。

您还可以将Array属性设置为“virtual”(没有后备字段,只需操作get&amp; set),例如:

//The property & field used for binding and editing
private readonly ObservableCollection<GPASemestre> _ObservableSemestre = new ObservableCollection<GPASemestre>();
public ObservableCollection<GPASemestre> ObservableSemestre { get { return _ObservableSemestre; } }

//The property used for serialisation/deserialisation
public GPASemestre[] Semestre
{
    get
    {
        return ObservableSemestre.ToArray();
    }
    set
    {
        ObservableSemestre.Clear();
        foreach (var item in value)
        {
            ObservableSemestre.Add(item);
        }
    }
}