I have a list of data where each row is string string int. All of my data comes from plists as I am porting an iOS app. I also have another list which is string int double.
I’m using this tutorial to create my own rows for the list box.
I do not want to create a class for the row data so I don’t have the Transaction class and am unsure how to do the binding using a list of List. I’ll know of course if I am string string int or string int double.
Any reason you don’t want to create a class type for your row? This will make your life a million times easier when working with XAML.
Create a class type for your row and make it implement INotifyPropertyChanged. Once your class implements
INotifyPropertyChanged, it is easy to bind to it in your view.Let’s assume you are following the MVVM pattern. This means you will have a View (which you are coding in XAML) and a ViewModel. This view model is going to be a class that implements
INotifyPropertyChangedas well. In the constructor of the view, you will set theDataContextproperty to a new instance of your ViewModel class.Now for the list part. In the ViewModel, create an
ObservableCollection<MyRow>. AnObservableCollection<T>is a collection type that too implementsINotifyPropertyChanged. Or in your case, it sounds like you have a list of these items. If this is true, I recommend encapsulating the list with another custom class. It makes the binding more natural with the MVVM pattern. You can double-nest an ObservableCollection but you’ll have to make the decision whether or not that is easy to read and understand. For instance,ObservableCollection<ObservableCollection<MyRow>>. In the following example, I assume you create a class (MySet) to hold the rows as opposed to double-nested lists.Custom Row Class
Row Set Class
View Model
Now in your XAML, you have some options on how you want to display the nested nature of the list. I recommend the ItemsControl container. All you have to do is set the
ItemsSourceto your ViewModel’s property name and then override theDataTemplate. If you do this, then each item will have the data context of the inner list. Repeat with another ItemsControl and then you’ll have the item context be equal to each of the inner items (MyRow).View’s XAML
And don’t forget to set the DataContext of the view to the ViewModel.
View’s Code Behind
The code above is not tested and only gives you a general idea of where to go with this.
Update
If you aren’t wanting to create a class but merely bind to an array, you can do this too. The binding path allows for indexer syntax. You can enter in the XAML
Text="{Binding Path=MyArray[0]}".If you have an array of an array, the XAML could look as follows:
A couple other options are to use Converters to translate the values in the array into some string format or to provide a property on the row class that is responsible for formatting the members appropriately. Are you just printing the array elements as strings in a sequence? Then just create a custom property on the row class called ‘FormattedValue’ and have it return the ToString() concatenation of all the relevant properties.