How to expose DataGrids Columns property for Binding with property in Viewmodel?
In my Viewmodel, depending upon given datatyp开发者_如何转开发e 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 ?
You cannot do that, but there is a workaround, check this solution: Answer in Stackoverflow
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.
精彩评论