I need to build and display a WPF data grid from a collection of string array that i got from a txt. The problem is that i don't know a priori which will be the number of columns i.e. the number of item in the single array. So i defined in my xaml <DataGrid Grid.Row="2" ItemsSource="{Binding Path=Rows}" />
I was trying to fill it in my View Model, but i cannot simply put my collection (Observable Collection) of array as item source, since the datagrid will display only blank rows.
I can also use other approach over the Observable collection since i produce my array in the same method
this is my Observable Collection:
ObservableCollection<string[]> Rows = new ObservableCollection<string[]>;
in this method i fill the collection
foreach(ListViewItem item in wsettings.lista)
{
TextBlock line = item.Content as TextBlock;
string txt = line.Text;
string[] x = txt.Split(stringSeparators, StringSplitOptions.None);
Rows.Add(x);
}
Please don't mind the first part before the split. I take my data from a listview of text block that i used before(I have my reason).
EDIT1: made the code more readable
EDIT2: the header must be a combobox that a user must set
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…