Due to some reasons I am using unbound fields to create columns in my XamDataGrid. These unbound fields use a databinding to show data.
UnboundField unboundField = new UnboundField{ BindingPath = new PropertyPath("Values[" + columnIndex + "].Value"), BindingMode = BindingMode.TwoWay}
In addition to that I am binding a ObservableCollection<RowData> to the XamDataGrid, which has the property Values (an array of objects) that is used in the binding of the unbound fields.
When I now remove a column from my collection of row data (e.g. remove the last element in the Values array of all row datas), I am getting an exception in the output window, because the unboundField in the XamDataGrid references to an entry in the Values property that does not exist anymore.
Then I tried to remove the unboundField form the fields collection of the XamDataGrid before removing the data from my Values array. But this seems to work neither.
So, how can I remove a unboundField correctly?
thanks, Florian
Florian,
Yes, these binding expression errors definitely slow down the application.
You would have to check if the collection has the ensured capacity to supply data for the unbound fields. You still see exceptions in my sample, because I am removing the first item in the collectiona and the first field in the collection.
What you actually need to do is change the binding path of the fields according to the removed items. For example, if you have 5 items and remove one of them, the last unbound field will still point to Index 5, so you need to remove the last one or change its binding path.
So is there no way to avoid these exceptions? It think it slows down my application. Maybe there is a way to remove the fields along with the binding objects. And afterwards the data objects is removed. So there should not be any exception.
Florian
These exceptions are expected, because the bound object is deleted. The UnboundField is creating a binding object in order to bind the cell's value. When you delete items from the collection, bound objects are deleted and therefore the exceptions is thrown.
Hello Alex!
I am removng the fields from the FieldLayouts[0] collection which is the one I added the fields. So this seems the right fields collection to me.
As I read your answer, I found that I missed something important. The clearence of the field works for the visible part of the grid. The column disappears. Just like it is doing in your example.
BUT: Did you notice the exceptions in your sample project? You can see them in the ouput window. This is what makes me banging my head on the table. I get lots of those exception and it really slows my application down. How can I avoid those exceptions?
Exception:
System.Windows.Data Error: 16 : Cannot get 'Item[]' value (type 'Object') from 'Interests' (type 'List`1'). BindingExpression:Path=Interests[2]; DataItem='Person' (HashCode=26038915); target element is 'ValueHolder' (HashCode=51095287); target property is 'Value' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException
Thanks and a nice weekend for now,
Hello Florian,
You should be able to delete a field from the Fields collection and the field should be remove from the layout. Can you confirm that you are removing it from the correct field layout?
I was not able to reproduce this behavior. Please find the sample project attached and let me know if I am missing something out.