0

I have a DataGrid with Combobox

<DataGridComboBoxColumn Header="Header 2" ItemsSource="{Binding Pet}">

Project based on MVVM pattern. When I try to display data on TextColumn, it works

<DataGridTextColumn Header="Header 1" Binding="{Binding ID}" />

But, I don't know, how to bind the data for DataGridComboBoxColumn.

The code of Model:

    public string ID
    {
        get { return _id; }
        set { _id = value; NotifyPropertyChanged("ID"); }
    }
    public string[] Pet
    {
        get { return _pet; }
        set { _pet = value; NotifyPropertyChanged("Pet"); }
    }

3 Answers 3

6

Instead of using the <DataGridComboBoxColumn> try using <DataGridTemplateColumn> like this

<DataGridTemplateColumn Header="ID">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=DataContext.Pet, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" SelectedItem="{Binding ID}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

In this example, I have bound my Combobox's item source as Pet array collection defined in my current ViewModel and the selected item as the ID property of the List<> collection bound to the complete DataGrid.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer, but it doesn't work for me. I have a model public class ListItem wich contains property public string[] Pet and I try to get datas from it. Should I need to change type of property Pet? Also, I bound <DataGrid ItemsSource="{Binding ListItems}"...> before and that's why binding for <DataGridTextColumn Header="Header 1" Binding="{Binding ID}" /> works fine.
Assuming that ListItems is a collection of ListItem class. Then just ignore the RelativeSource part in the binding and simply write <ComboBox ItemsSource="{Binding Path=Pet}"/> and it should work
0

DataGridComboBoxColumn and DataGridTextColumn won't participate in logical tree. So binding won't work in this. You can overcome this in the below two ways, 1. Define your view model as static resource and StaticResource binding 2. You can use RelativeSource binding which will also work for your case

Refer the below blog for more information about how to do, http://blogs.msdn.com/b/vinsibal/archive/2008/08/28/wpf-datagrid-working-with-datagridcomboboxcolumns-part-2.aspx

2 Comments

I tried to bind as a StaticResource ItemsSource="{Binding Source={StaticResource Pet}}" and I received an error XamlParseException
you can refer the below link, lfhck.com/question/378365/…
0

I've had the same problem for a day now, and have found this to be a better solution, as it doesn't mess up rendering of the combobox:

            <DataGridComboBoxColumn Header="ID" SelectedItemBinding="{Binding ID}">
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Pet}"/>
                        <Setter Property="IsReadOnly" Value="True"/>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Pet}"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.