WPF data binding - DataGridComboBoxColumn and ObjectDataProvider - wpf

I'm trying to code a WPF 4.0 DataGrid that matches a database table, with two entries in particular GroupID and SectionID... GroupID contains a collection of groups, and SectionID should be built from a table that takes a {GroupID, SectionID} keys and returns section information.
Binding the DataGridComboBoxColumn to the groups table to show the group names is easy using an ObjectDataProvider, but I can't work out how to do the sections given the fact I need to pass the relevant GroupID for the item into the GetSections() method the ObjectDataProvider is bound to...
Anybody else solved something similar?

Here is a link that explains a lot about a DataGridComboBoxColumn: link. Also, I would try to bind the DataGrid to an ObservableCollection of a class in which you add properties for each of your columns and then bind each column to a property of the class. By the way I've only done this in C# 3.5...

Related

WPF: DataGrid with a tree-column in an MVVM Windows application

Here is a rephrased version of my issue:
Here is a screenshot of the result I'm looking for (from an older, non C#/WPF version of the application):
Looks like all the other grid-tree-views and what ObjectListView provides. But unfortunately I have some rules I have to follow:
WPF
MVVM
Just a DataGrid and its components must be used (no TreeView, ListView, etc.)
The Items are hold in an ObservableCollection<T> where T.Property1, T.Property2 etc hold the data for the columns of the DataGrid.
The lower Levels of an item are hold in T.PropertyChilds of type ObservableCollection<T> and are not known/filled beforehand. They must be loaded on the first exand of the item.
Here is a screenshot of similar data and what I've got so far in WPF/C#:
It looks like a standard DataGrid. But which columns it can hold, which property of T the columns are showing, which columns are currently visible, etc is defined by a own framework around the DataGrid which basically just adds columns to the public ObservableCollection<DataGridColumn> Columns Collection of a System.Windows.Controls.DataGrid.
My task is now to do a new class derived from DataGridColumn which can display and load the hierarchical data structure of my ViewModel described above.
The main problems I'm facing are as far as I can tell:
- how to visibly display the levels like a treeview in a DataGrid column
- how to process the loaded data from childs so they are shown as in the first screenshot above. Do I have to do a flat ObservableCollection<T> where I add the loaded ChildData and some extra properties like level and parent - or is it possible to define a kindo of a hierarchical data template for a DataGrid?
Thanks a lot
Soko

Problems binding Self-Tracking Entity's Navigation Property WPF

I have a WPF application consuming data using Entity Framework 4 and Self-Tracking Entities. In which I have a window with 2 controls in it, one that shows the "Details" portion of an object using a ContentControl and templates from a merged resource dictionary. Another with a ListBox of Groups the object in question belongs to and a ComboBox of available groups it could belong towith a button wired up via a command to the control to add/remove items from the bound collection of Groups based on the SelectedItem of the ComboBox. All this is bound together by DependencyPropertys.
In my Window I have DP's for the Object, EditedItem we are editing and a read only property with a List of Group of the groups it could belong to and bind that to my controls via XAML.
SO....
If I create a new instance of one of my entities, set it's properties like so: (Really this is the exact code)
Employee employee = Context.CreateObject<Employee>();
employee.Name = "Joe Nobody's Brother Steve";
employee.Active = true;
employee.Username = "snobody";
Group group = Context.CreateObject<Group>();
group.Name = "Losers";
group.DisplayName = "Spirit Squad";
employee.Groups.Add(group);
And set it as my Window's EditedItem it works FLAWLESSLY!
If I however fetch this exact same entity from my Database the Groups ListBox is empty.
Any ideas?
It turns out I had made a mistake else where:
I needed to call:
ObjectContext.LoadProperty(entity, navigationProperty);
on my navigation properties for them to get populated. I think this has something to do with my objects all being derived from a core object and the fact that I select them using OfType on the ObjectSet of the core object. Or it could be behavior but I would think I would have encountered it before now.
But hey I'll take working, and this is easy enough to integrate into my selection methods and properties.
Chalk this one up to ignorance of EF4.

How to display in each ListBoxItem textblocks with binding to a filelds of a different tables

Greetings to all and sorry for my English!
I have a ListBox, it's ItemsSource = myClientsList.DefaultView. The Items of ListBox have a template (ControlTemplate), that is defined in a in a separate resource file.
Every Item contains a little TextBlock's, Text -property of each have a binding to fields of my Object myClientsList.
I need to add in a this item template more TexBlock's and each of them must have binding to fields of another my class myOrdersList. - (So I wish to view on each line of ListBox information from different tables of my database - this is a question).
Problem in that that ListBox's ItemsSource have a link to object myClientsList and I cann't set myOrderList to ItemSource of same ListBox. So i must find a way to specify TextBlock.DataContext wich inside ControlTemplate or how it's possible to solve this problem in another way?
p.s. I'm a new in .Net and WPF and probably have a mistakes in my explanation - sorry for it.
It sounds like you have a DataGrid type of display and want to add more columns in order to display the order information for a given client. If this is the case, you are going to have to do a couple of things. First, you will need to create a composite object that stores information for both entities into a single object (so each row of your control has all the data it needs to display). Secondly, I would recommend using an actual DataGrid control to display rows instead of templating a ListBoxItem. The ListView with a GridView built into the framework isn't great, so I would recommend the WPFToolkit's DataGrid for a free option.
There are two issues here, if I've understood the question: how do you create a single collection containing both Clients and Orders, and how do you display Clients and Orders in different ways within the same ListBox?
Regarding the first, you can do this using a CompositeCollection.
Regarding the second, define two DataTemplates instead of a ControlTemplate. As the key of each DataTemplate, use the type of the object it is going to present e.g.
<DataTemplate x:Key="{x:Type local:Client}">
Alternatively, use ItemsControl.ItemTemplateSelector to explicitly point at different DataTemplates depending on the type of item. Ot if you really have to use ControlTemplates, check out ItemsControl.ItemContainerStyleSelector.

TreeListView with Columns generated and populated by ObservableCollection

Does anyone known (or even has an example) of a WPF based TreeListView that can generate its colums by databinding to the ObservableCollection of its tree items?
For example the databound model is a tree consisting of Employee instances representing the supervisor hierarchy of a company. Each employee addtionally has a ObservableCollection of Responsibility instances (Properties: ResponsibiltyName, ResponsibleSinceDate). Now I want a separate column for each ResponsibiltyName found in any of the databound Employees and the column value should be populated with the ResponsibleSinceDate. If an Employee does not have a certain Responsibilty the column value shall be left blank.
How would one usually approach such a problem in WPF?
You may want to take a look at the following answer: How do I bind a WPF DataGrid to a variable number of columns?
Here a solution involving a CompositeCollection is suggested: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a4c5b2de-260c-49d0-b4ff-cca6ee4e8b08/
Here a solution involving a HierarchicalDataTemplate is suggested: http://blogs.msdn.com/karstenj/archive/2005/11/02/488420.aspx
Hope this helps!

WPF Binding relational ComboBox's from L2S with filtering on all of them

in a WPF project with Linq to SQL, if you use the O/R - designer to create a simple structure with 3 that are all tied with forgin key relataions like so:
Customer <-- Orders <-- Items, and say i want a simpe window with 3 syncronized comboboxes
when you select a customer you see only his orders and when you select an Order you see only the Items for that Order. all of this is simple....
Lets say i want to add filtering capablities to all the comboboxes. how would i do that if i want to use the entity objects from the LINQ dbml file?
Edit - Elaborating on filtering.
i would like to filter the in memory collection without the need to query the database again, the filter can be a textbox that is over the combobox, that doest matter, my problem is that i cant filter the comboboxes because the are bound to an EntitySet through the L2S and dont implement filtering.
Thanks,
Eric
I would look into using CollectionViewSource. Bea Stollnitz has a good primer on it here and I used this blog post to show me how to filter. This will let you filter and sort without having to use the database and is pretty fast.

Resources