Use deserialized XML data in TabController + DataGrid

半腔热情 提交于 2019-12-12 05:48:22

问题


After this: Acess to a DataGrid through C# code and manipulate data inside the DataGrid I decided that i should deserialize my XML data and use it that way because i need basic CRUD manipulation in my application.

I already have my xml data class (using XSD tool, you can find the class here -> http://pastebin.com/6GWFAem6) and have my data deserialized, problem is:

  1. I need a TabControl with as many tabs as Semestre in my xml, each tab will have GPASemestre.Nome header.

  2. Inside each tab i need a DataGrid with Cadeiras of that specific Semestre.

  3. I need to be able to CRUD data in the DataGrid and the tabs.

Questions:

  1. To solve all of this what do you think is best? Creating everything (tabs + datagrid) and make the necessary binds (which i don't really know what they will be) / populate the DataGrid somehow, in C# only? Or there is a way to simplify code in C# using XAML?
  2. Cadeiras are stored in arrays so, each time i add a new one, i need to create a new array (or create a new array with more spaces and manage it), i already saw some questions here where ppl used List's but where having troubles with it, is it possible to use a list or not? If so, what do i have to change in the XSD auto generated class?

Thanks in advance!


回答1:


I would suggest the use of data-binding and data-templating (read those if you have not yet done so) for as much as possible, for that to work well the auto-generated classes need to be adjusted to support it.

The first step is implementing INotifyPropertyChanged in all the properties which are not collections so that if the property is changed the UI will update automatically. Only use arrays for deserialisation at best, after that copy the elements to a property which is of type ObservableCollection<T>, or any other collection which implements INotifyCollectionChanged so that the grids update when a new item is added to the collection and never touch the arrays again.

You could also make the Array property "virtual" (has no backing field, just manipulates on get & set), e.g.:

//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);
        }
    }
}


来源:https://stackoverflow.com/questions/6977397/use-deserialized-xml-data-in-tabcontroller-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!