How to set ItemSource of ComboboxColumn in Datagrid to a List in ViewModel

Jan 27, 2009 at 1:37 PM

- I have a View bound to ViewModel via DataContext
- in the View is a DataGrid with its ItemSource bound to ViewModel.Customer.TelefonNumbers
- in the DataGrid is a TextBoxColumn bound to the TelefonNumber (thats ok: Binding the Number to a TextboxColumn in the grid works)
- in the DataGrid is a ComboBoxColumn that should bound with its ItemSource to a Property 'NumberTypes as IList(Of String)' and its SelectedValue bound to 'NumberType as String'

How do I have to set the binding for the ItemSource? All I tried in the last few hours gives me a BindingException.

<toolkit:DataGrid ItemsSource="{Path=SelectedCustomer.TelefonNumbers}" CanUserAddRows="True" CanUserDeleteRows="True" AutoGenerateColumns="False">
        <toolkit:DataGridTextColumn Binding="{Binding Number}" Header="Number" />
        <toolkit:DataGridComboBoxColumn x:Name="dgc4"  ItemsSource="{Binding     ?...?              , Path=SelectedCustomer.NumberTypes}" SelectedValueBinding="{Binding NumberType }"  Header="Art" MinWidth="50"/>







Jan 27, 2009 at 6:36 PM
You'll have to set the ItemsSource in the EditingElementStyle and the ElementStyle.  For example:



<toolkit:DataGridComboBoxColumn Header="Field" 
    SelectedItemBinding="{Binding Field}"
    ElementStyle="{StaticResource FieldValuesNonEditingStyle}"
    EditingElementStyle="{StaticResource FieldValuesStyle}">


x:Key="FieldValuesNonEditingStyle" TargetType="ComboBox">
    <Setter Property="ItemsSource" Value="{Binding Path=FieldValues}" />





<Style x:Key="FieldValuesStyle" TargetType="ComboBox">
    <Setter Property="ItemsSource" Value="{Binding Path=FieldValues}" />
    <Setter Property="IsEditable" Value="True" />
    <Setter Property="StaysOpenOnEdit" Value="True" />


Feb 13, 2009 at 11:58 PM
Edited Feb 13, 2009 at 11:59 PM
I figured I should continue this thread as this was what essentially solved my problem.  I am using a DataGrid in a UserControl that is bound to my EntryListViewModel.  The EntryListViewModel contains an ObservableCollection<EntryViewModel> of viewmodels that encapsulate the actual entry business objects.  It is this collection which is what the DataGrid is bound to. 

The main view model has the list of accounts which are intended to be shown in a drop down list for the Account field of each entry.  The EntryListViewModel has a property called AccountList which supplies this list.  To make things simple each EntryViewModel item can access the AccountList property from the EntryListViewModel through a _parent field, so EntryViewModel also provides AccountList as a property.  So then I tried to do some simple databinding the a ComboBox and first tried this:

<my:DataGridComboBoxColumn Header="Account"  SelectedItemBinding="{Binding Path=Account}" DisplayMemberPath="Name" ItemsSource="{Binding Path=AccountList}" >

That did not work I got errors like this when I click on the combobox and the box has nothing in it.  It does not populate with the objects from the collection:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=AccountList; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=23741667); target property is 'ItemsSource' (type 'IEnumerable')

So I found this post and tried it the way you've shown above:

<my:DataGridComboBoxColumn Header="Account" SelectedItemBinding="{Binding Account}" SelectedValuePath="Name" DisplayMemberPath="Name" >


<Style TargetType="{x:Type ComboBox}">

<Setter Property="ItemsSource" Value="{Binding Path=AccountList}" />

<Setter Property="IsEditable" Value="True" />

<Setter Property="StaysOpenOnEdit" Value="True" />




<Style TargetType="{x:Type ComboBox}">

<Setter Property="ItemsSource" Value="{Binding Path=AccountList}" />




Now this works, though I do still get the same error message, but only once when I first click on the combobox.  It is populating the box though so I am happy about that.

So what I would like to know is why do I have to do all this work?  The first example should honestly be more than functional.  Why do I have to respond to style triggers to get the binding to work here?  It seems like a whole lot of XAML to do something extremely simple.

(I would also like to know why this edit box is so frustratingly hard to use and how you are formatting the code on your posts ;-)