I am trying to create a grid with banded layouts which will have a different count of bands and columns every time the grid is loaded. This means that the data source for the grid and the columns are to be bound at run-time.
I have 3 classes, A, B, C.
A has some properties and a list of type B.
B has some properties and a list of type C.
C has some properties, one of them being MyName (which is publicly accessible).
My items source for the grid control is a list of class A, since I intend to show some values from all classes in this hierarchy.
List<A> abc = new List<A>();
...
MyGrid.ItemsSource = abc;
Now, every time the grid is being loaded, I am deleting all existing bands and columns and re-creating them (since the number of bands and columns are changing based on an external event). In the process, I need to bind one of the columns to C.MyName, however, when trying to do this:
GridColumn newColumn = new GridColumn();
newColumn.Binding = new Binding("B.C.MyName");
newColumn.Header = "Name field";
myBand.Columns.Add(newColumn);
MyGrid.Bands.Add(myBand);
...
I do not see any data entry in the column, but the column along with the band is present in the grid. The rows are empty.
I am relatively new to the concept of Binding and unable to understand why this is not working. Can anyone please help?
Thanks.
I found the solution by trial and error.
So, to create the relevant bands and columns, I had been iterating through the lists.
Assume that i
is being used to iterate through the list of B and j
is being used to iterate through the list of C (which is contained within B, as described in the question).
To bind a property of C to a specific column, all we need to do is this:
... newColumn.Binding = new Binding(String.Format("B[{0}].C[{1}].MyName", i, j)); ... myBand.Columns.Add(newColumn); ...
The rest remains the same.
I had missed out in identifying from the data source which elements of the list we need to consider to bind to the column. The String.Format() does that for me [ Column.Binding = new Binding("path"); ]
[ Column.Binding = new Binding("path"); ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.