Let me get straight down to it. I have a DataGrid with a few set columns, like this:
<ctrl:DataGridWithFooter
FooterRowsCount="1"
x:Name="MyGrid"
CanUserDeleteRows="False"
CanUserSortColumns="True"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=Model, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding Path=Idle}">
<DataGrid.Columns>
<DataGridTextColumn Header="Level"
Binding="{Binding Path=Name, Mode=OneWay}" />
<!--Snip-->
</ctrl:DataGridWithFooter>
And a model as you’d expect. As the application runs, though, I have the situation where I’ll add new columns that reflect an element in a list in the model, so I get a binding expression like
<DataGridTextColumn Binding="{Binding Path=Attributes[0]}" />
or something similar. Displays and sorts fine on its own, but I ran into an issue where I needed to force a row for my totals to the bottom, which I ended up needing to use a bit of code I found online to get it working ok (available here, works as advertised). However, I found his sorting classes in this new customised DataGrid class crash out with my list element bindings. My question is, how would I modify this constructor
public PropertyAccessor(Type targetType, string property) {
IsList_ = false;
mTargetType = targetType;
mProperty = property;
PropertyInfo propertyInfo =
targetType.GetProperty(mProperty);
mCanRead = propertyInfo.CanRead;
mCanWrite = propertyInfo.CanWrite;
mPropertyType = propertyInfo.PropertyType;
}
In order to get all the information I need to be able to reflect out and sort on the list elements?
I am glad to see that u used my sample (it’s my blog). One of the ways to accomplish this – is to create a public getter for specific attribute. Not very convenient may be. Cuz PropertyAccessor requires property. Or may be to parse “Attributes[0]” get firstly value of the property and than get element with index [0]
Also I’ve decided to create another solution for this task. The main idea is to create a wrapper for datagrid. Generally it looks like:
Footer is a Grid with textboxes (it can contain multiple rows). Columns in this Grid are synchronized with Columns of DataGrid. Also it allows to create footer with merged cells.
In this solution you don’t need to make any moves for sorting. It will be accomplished by DataGrid.
Here is how it looks like. I will share the code on blog a little bit later. You can get me in touch on my blog.