Click here to Skip to main content
15,392,679 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to bind a combobox to a list in my VM,
the combobox is located in a datagrid, this datagrid is binded to another datagrid.

How should I set up binding?

All binding that is set up now works just fine, exept for the binding on the combobox.

I can't seem to find my datacontext that is set in the mainwindow...

This is what i have so far (It does'nt work ofcourse..)

What i have now, in xaml:
            <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding ListBarcode}">
                <DataGrid.Columns >
                    <DataGridTextColumn   Binding="{Binding Path=Name,Mode=TwoWay,
                        ValidatesOnExceptions=True}" Header="BarcodeName" Width="Auto" />
                    <DataGridTextColumn Binding="{Binding Path=Number,Mode=TwoWay,
                        ValidatesOnExceptions=True}" Header="BarcodeNumber" Width="Auto" />
                    <DataGridComboBoxColumn Header="Brand" Width="1*" MaxWidth="100"
                                            ItemsSource="{Binding Path=DataContext.ListBrands,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window},
                        AncestorLevel=1}}" />

Thanks in advance!

Answer: WPF nested Datagrid in datagrid details {binding}[^]
Updated 9-Mar-11 22:26pm
Seth007 9-Mar-11 9:13am
Venkatesh Mookkan:
Ok, thanks for pointing that out, seems I misunderstood how the sytem works :/
Venkatesh Mookkan 9-Mar-11 23:18pm
Nobody down-voted the question. They have voted 4. That means Good question.

Seth007 wrote:

<DataGridComboBoxColumn Header="Merk" Width="1*" MaxWidth="100" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window, AncestorLevel=1}, Path=DataContext.Merkenlijst}" />

I see a typo mistake in the your XAML. Merkenlijst is this correct or is it suppose to Merkenlist?
Seth007 10-Mar-11 3:41am
It is correct, "MerkenLijst" is the Dutch version of "MerkenList", I'll change the code sample to English to make it more readable, thanks for bringing this to my attention.
Because DataGridComboBoxColumn doesn't inherit from FrameworkElement it is not rendered in the visual tree, so you can't use relative binding.

The columns in the datagrid dont have a datacontext, as they are never added to the visual tree. Once the grid is drawn the cells have a data context and you can set the combo boxes items source in them using normal bindings (not static resources...)

<DataGridComboBoxColumn Header="Brand" Width="1*" MaxWidth="100" TextBinding="{Binding Path=Merk}">
                                      <Style TargetType="ComboBox">
                                          <Setter Property="SelectedItem" Value="{Binding Path=Merk}"/>
                                      <Style TargetType="ComboBox">
                                          <Setter Property="ItemsSource" Value="{Binding Path=Merklijst}" />
                                          <Setter Property="SelectedItem" Value="{Binding Path=Merk}"/>

Some links with some code and explenations:[^][^][^][^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900