0

In my Viewmodel, depending upon given datatype I am generating column( using reflection get all properties of datatype and use them to create appropriate datagrid column) and adding it to Columns collection. Now I want to bind this Collection to my datagrid's columns property, like below:

<DataGrid Columns="{Binding VMColumnCollection}" />

Please let me know how to achieve this without breaking MVVM ?

Nikhil Vaidya
  • 93
  • 2
  • 8

2 Answers2

0

You cannot do that, but there is a workaround, check this solution: Answer in Stackoverflow

Community
  • 1
  • 1
Mohammed A. Fadil
  • 9,205
  • 7
  • 50
  • 67
  • I tried the solution provided in that thread, but it is not working. I am using 3 instances of datagrid on same View and have different datatype for all three. but all 3 datatype have a property with same name. It throws exception at line dataGrid.Columns.Add(column); it says that *** column already exist. – Nikhil Vaidya Apr 27 '11 at 11:11
0

I got the solution, the problem was not with the attached property. Actually I was using datagrid inside TabControl.But when I was switching from first tab to second tab and then again back to first, an exception was getting generated from attached property that 'column xyz already exist.....' but the Datagrid.Columns.Count was 0. This was very strange even though columns count is 0, i can't insert a column ( although I am not able to find why this happened).Then while debugging found that whenever i was switching back to my previous tab, constructor of view was called.Switching back to tab should not create new instance of View, I searched on internet and found below links

How to preserve control state within tab items in a TabControl

http://eric.burke.name/dotnetmania/2009/04/26/22.09.28

then I created a custom Tabcontrol as shown in 2nd link. Now it is working.

Community
  • 1
  • 1
Nikhil Vaidya
  • 93
  • 2
  • 8