Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a issue with DataGrid bound to ObservableCollection, where each column is a ComboBox. First ComboBox has ItemsSource binded to ObjectDataProvider. The other ComboBoxes are populated with items depending on the SelectedItem in first ComboBox. I'm using DataGridTemplateColumn\DataTemplate\ComboBox... The problem is that when I select an item from first ComboBox, SelectedItem is not set, because no items exist in the collection the DataGrid is bound to. DataGrid should create new row (add object to collection) as soon as I start selecting an item from first ComboBox. I can't add new row.

<DataGrid DockPanel.Dock="Top" ItemsSource="{Binding Path=Routes, Mode=TwoWay}"                  
                    AutoGenerateColumns="False"                                            
                    CanUserDeleteRows="True"
                    CanUserAddRows="True"    
                    IsSynchronizedWithCurrentItem="True"                    
                    GridLinesVisibility="All" 
                  >
            <DataGrid.Resources>                
                <ObjectDataProvider x:Key="Modules" ObjectType="{x:Type serv:PortPinDataService}" MethodName="GetModules"/>
            </DataGrid.Resources>
            <DataGrid.Columns>

                <DataGridTemplateColumn Header="Module" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Source={StaticResource Modules}}"
                                      DisplayMemberPath="Name"
                                      SelectedItem="{Binding Path=CurrentModule, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">                                
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>                    
                </DataGridTemplateColumn>
                
                <DataGridComboBoxColumn Header="Module" ItemsSource="{Binding Source={StaticResource Modules}}"
                                        SelectedItemBinding="{Binding Path=CurrentModule, UpdateSourceTrigger=PropertyChanged}"
                                        Width="80"    
                                        DisplayMemberPath="Name"
                                        >
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">                            
                            <Setter Property="Width" Value="80"/>
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                </DataGridComboBoxColumn>                

                <DataGridTemplateColumn Header="Signal" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=SignalsByModule}" DisplayMemberPath="SignalName" SelectedItem="{Binding Path=CurrentSignalRegister, UpdateSourceTrigger=PropertyChanged}"></ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Route to" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=Registers}" DisplayMemberPath="RegRouteInfo"></ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>


So my question is why a new object is not added to DataGrid collection if using DataGridTemplateColumn?

What I have tried:

If I change the first column to DataGridComboBoxColumn a new item is added to collection as soon as I start picking an item and SelectedItem is set.
Posted
Updated 27-Feb-19 4:24am

1 solution

 
Share this answer
 
Comments
Zmuro 1-Mar-19 3:51am    
I'm still having the same issue. The Routes collection which is bound to DataGrid ItemsSource is empty at the beginning and should be populated with Route objects via DataGrid, by selecting values from ComboBoxes. I hooked the CollectionChanged event to Routes and I don't receive this event if I select a value from fist ComboBox, therefore I know that a Route object was not yet created and I can't bind SelectedItem to CurrentModule which is a property in Route object. SignalsByModule and Registers are also a collection property in Route object.

Should I choose a different approach? Should I move all collections the ComboBoxes are bound to ViewModel?

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