WPF databinding - many to many - combobox, Entity Framework 4 [modified]
-
Thanks in advance. Without boring you to death with all the details - I have an EF model intended to track project requests. The hierarchy is that corporate divisions have departments; the departments submit project requests. The requests have to be categorized by risk and can fall into one or more categories. This last component is represented in the database via a Many-to-Many table incorporating the RequestIDs and the RiskIDs. EF eliminates the many-to-many table and shows the association between the Request and RiskCategory entities. I've created a WPF form that ALMOST works they way I want, showing the users the details of their requests. I'd like to add to the form a combobox within a listview, from which the users can select the categories that apply to a given request. Only the combobox will be in the ListView So..the CollectionViewSource for the RiskCategories table is categoriesViewSource, and the CollectionViewSource for the RequestItems is named RequestItemsRiskCategoriesViewSource The Datacontext of the ListView itself is set to RequestItemsRiskCategoriesViewSource, and the combobox's ItemsSource is set to the categoriesViewSource. Try as I might, I can only get the combox to display the available RiskCategories, not the ones already assigned to the current RequestItem. If there is one category assigned, then one combobox hows in the listview. If 3, then 3 comboxes appear. But each one displays the first category in the list, not the assigned categories. Changing the datatemplate to a Textbox bound to Category works just fine, but gives the user no way to choose a valid value
<ListView.View> <GridView> <GridViewColumn x:Name="categoryColumn" Header="Category" Width="80"> <GridViewColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Source={StaticResource categoriesViewSource}}" SelectedValue="{Binding Path=RiskCategory}" DisplayMemberPath="Category"> </ComboBox> </DataTemplate>
-
Thanks in advance. Without boring you to death with all the details - I have an EF model intended to track project requests. The hierarchy is that corporate divisions have departments; the departments submit project requests. The requests have to be categorized by risk and can fall into one or more categories. This last component is represented in the database via a Many-to-Many table incorporating the RequestIDs and the RiskIDs. EF eliminates the many-to-many table and shows the association between the Request and RiskCategory entities. I've created a WPF form that ALMOST works they way I want, showing the users the details of their requests. I'd like to add to the form a combobox within a listview, from which the users can select the categories that apply to a given request. Only the combobox will be in the ListView So..the CollectionViewSource for the RiskCategories table is categoriesViewSource, and the CollectionViewSource for the RequestItems is named RequestItemsRiskCategoriesViewSource The Datacontext of the ListView itself is set to RequestItemsRiskCategoriesViewSource, and the combobox's ItemsSource is set to the categoriesViewSource. Try as I might, I can only get the combox to display the available RiskCategories, not the ones already assigned to the current RequestItem. If there is one category assigned, then one combobox hows in the listview. If 3, then 3 comboxes appear. But each one displays the first category in the list, not the assigned categories. Changing the datatemplate to a Textbox bound to Category works just fine, but gives the user no way to choose a valid value
<ListView.View> <GridView> <GridViewColumn x:Name="categoryColumn" Header="Category" Width="80"> <GridViewColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Source={StaticResource categoriesViewSource}}" SelectedValue="{Binding Path=RiskCategory}" DisplayMemberPath="Category"> </ComboBox> </DataTemplate>
Are you using a viewmodel to supply data to the view? If the datacontext of the view is the viewmodel, you might have to use a relative source to access the proper data context. ex:
It really depends on the data context for the container of your control, but if the parent container or control has a datacontext set on a viewmodel, this might work for you. Change the Path to point to whatever the ObserveableCollection(?) that you are trying to bind to.
Cheers, --EA
-
Are you using a viewmodel to supply data to the view? If the datacontext of the view is the viewmodel, you might have to use a relative source to access the proper data context. ex:
It really depends on the data context for the container of your control, but if the parent container or control has a datacontext set on a viewmodel, this might work for you. Change the Path to point to whatever the ObserveableCollection(?) that you are trying to bind to.
Cheers, --EA
Thanks for the response. I'm a part time 'programmer' trying to learn C# and WPF after years (and years) of Excel VBA and some T-SQL. The MVVM learning process has been VERY slow for me as I completely lack experience in creating and, more importantly, programming with POCO classes. All my past work that required data access used direct connections to the db and calls to stored procedures. So, in a long winded fashion I'm replying that this WPF attempt is not using MVVM concepts and I'm trying to databind to EF EntitySets and EF entities. Because of the issue in the original question and some other problems, I've paused the project while trying to get some MVVM education. Still am struggling to find any non-trivial examples/explanations of how one goes from EF or LinqToSQL to MVVM models. While there is a tremendous amount of quality MVVM information available, here and elsewhere, there seems to be an ongoing assumption that the readers are well versed in getting from a database datastore all the way through the construction of the models. I see very little discussion about what concepts are important to incorporate, what the approach should be, etc. Again, thanks for your response. Maybe I'll be back after making progress on the MVVM front.