Select to view content in your preferred language

Difficulty binding checkbox using MVVM Patten

2977
1
04-01-2011 08:01 AM
tandrews
Deactivated User
Hello all,
I'm trying to get my head around the MVVM pattern; the journey has presented some interesting stumbling blocks.  What I have here is an effort to convert existing code architecture to leverage MVVM.

Basic components:  SearchResultWindow.xaml, SearchResultWindow.xaml.cs, SearchViewModel.

General Overview:
I have a DataTemplate defined as a resource in my SearchResultWindow.xaml file.  In the codebehind for this page, I have a method that builds a items in a treeview dynamically based on data returned from a WCF.  Within each treeview item, we dynamically create a DataGrid and add it to the treeview item.  When one of these treeview nodes is expanded, the DataGrid contained therein will be populated via a method in the SearchViewModel that calls a WCF Service.  The datagrid???s columns are built dynamically because it is unknown how many columns are needed based on the data coming back from the WCF Service.  Additionally, we use the DataTemplate resource from our xaml file to dynamically create a DataGridTemplateColumn containing a checkbox and adding this column to the datagrid.

Here is the problem: The DataGrid binds to SearchViewModel.FeatureAttributeDataGridRows which is a collection of dictionary items that have been converted using IValueConverter while binding the dictionary object to the column.  Although this works well for the DataGridTextColumns I cannot seem to find a way to achieve the CheckBox binding for the CheckBoxes contained in the DataTemplateColumn.  These CheckBoxes must be bound to the Feature.IsSelected property, which is not a member of the FeatureAttributeDataGridRows collection.  The ???Feature??? class is an inner class contained in the SearchViewModel.  A collection of Features exist as a property of the FeatureClass class (another inner class in SearchViewModel), as you will see in the GetFeatureClassFeatures method.  I have not included the code that makes up these inner classes.  Following an MVVM pattern, how should I bind the IsSelected property of the Feature class object to the DataGridTemplateColumn AND bind the other columns of the datagrid to the SearchViewModel.FeatureAttributeDataGridRows?  Any help getting these CheckBoxes bound would be appreciated.  Following are some code snippets:
Beginning with the xaml, I have a DataGridTemplateColumn setup with the intention of binding the checkboxes contained in this column to a property in the SearchViewModel.



*Codebehind for SearchResultWindow:
In the codebehind I have a method that creates and populates a treeview:
I???ve slimmed this down for the sake of brevity; the main component of interest here is DataGrid dgFeatureDetails
This DataGrid will get created several times in this loop, one for each top level node in the tree.




* SearchViewModel Snip





Any help here will certainly be appreciated!  TIA
0 Kudos
1 Reply
JenniferNery
Esri Regular Contributor
This is a lot of code to go over... and binding errors are usually tricky to find.

I think you need to check first that your DataGridTemplateColumn binds to a collection with each item containing the two public properties IsSelected and ObjectId. They can either be DependencyProperty or the class they belong to implement INotifyPropertyChanged. These public properties having their own getter and setter. You can also try adding Mode=TwoWay. If they are part of Attributes (dictionary), you need to surround them with square brackets ([key]).  The DataContext of your ItemTemplate should be what you expect so you can determine the proper way to get/set these properties.

Here's a good troubleshooting list: http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/debugging-data-bindings-in-a-wpf-or-silverl...
0 Kudos