Click here to Skip to main content
15,867,834 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:
XML
<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <Grid>
            <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding ListBarcode}">
                <DataGrid.Columns >
                    <DataGridTextColumn   Binding="{Binding Path=Name,Mode=TwoWay,
                        ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,
                        ValidatesOnExceptions=True}" Header="BarcodeName" Width="Auto" />
                    <DataGridTextColumn Binding="{Binding Path=Number,Mode=TwoWay,
                        ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,
                        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}}" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </DataTemplate>
</DataGrid.RowDetailsTemplate>




Thanks in advance!

Answer: WPF nested Datagrid in datagrid details {binding}[^]
Posted
Updated 9-Mar-11 22:26pm
v5
Comments
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?
 
Share this answer
 
v2
Comments
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...)

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




Some links with some code and explenations:
http://wpf.codeplex.com/Thread/View.aspx?ThreadId=46627[^]
http://stackoverflow.com/questions/1633800/wpf-datagrid-datagridcomboxbox-itemssource-binding-to-a-collection-of-collection[^]
http://stackoverflow.com/questions/1903057/wpf-datagridcomboboxcolumn[^]
http://blogs.msdn.com/vinsibal/archive/2008/08/14/wpf-datagrid-dissecting-the-visual-layout.aspx[^]
 
Share this answer
 
v5

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