andrew
andrew

Reputation: 1244

datagrid showing one new row, but not any subsequent

I’ve got a datagrid with one column displaying a combobox. At present the new row is shown underneath existing rows – as expected.

<grid> 
<DockPanel Grid.Column="0" Grid.Row="0">
        <TextBlock DockPanel.Dock="Top" Text="Role Groups"/>
        <DataGrid DockPanel.Dock="Bottom" Name="dgRoleGroups" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalAlignment="Left" ItemsSource="{Binding ListSecurityUserRoleGroup}">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Role Group" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding ListSecurityRoleGroup, 
                                RelativeSource={RelativeSource AncestorType=UserControl}}" 
                                      DisplayMemberPath="Description" SelectedValuePath="ID"  
                                      SelectedValue="{Binding RoleGroupID}”/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
</Grid>

Where ListSecurityUserRoleGroup is an ObservableCollection of:

public class tbl_SecurityUserRoleGroup_Row
{
    public int UserID  { get; set; }
    public int RoleGroupID { get; set; }
}

And ListSecurityRoleGroup is a list of:

  public class tbl_Security_RoleGroup_Row 
{
    public int ID { get; set; }
    public string PublicID { get; set; }
    public string Description { get; set; }
}

In the code behind I’ve got:

dgRoleGroups.DataContext = ListSecurityUserRoleGroup;
dgRoleGroups.ItemsSource = ListSecurityUserRoleGroup;

The pic below shows that the binding for the first row is working; and I’ve got a new row and can pick a value for that.

enter image description here

However, I then cannot get another new row. This is the problem I’m trying to solve.

From reading other posts, I suspect I’m missing something in the realm of IEditableObject, INotifiyProperyChanged or because there is only one column in this datagrid, maybe need to trigger something from the combobox SelectedChanged – like check to see if a blank row is visible and if not, create one ?

I've not found a post matching my issue but i'm sure it's there...

There may be other solutions that do not involve datagrids however once I’ve got this working, my next task is a datagrid containing 2 columns of comboboxes, which will need to work there.

Upvotes: 2

Views: 392

Answers (1)

user1064519
user1064519

Reputation: 2190

You just need to add an edit template :

        <Grid>
            <DockPanel Grid.Column="0" Grid.Row="0">
                <TextBlock DockPanel.Dock="Top" Text="Role Groups"/>
                <DataGrid DockPanel.Dock="Bottom" Name="dgRoleGroups" AutoGenerateColumns="False"
                          CanUserAddRows="True" CanUserDeleteRows="True"
                          HorizontalAlignment="Left" ItemsSource="{Binding ListSecurityUserRoleGroup}">
                    <DataGrid.Columns>

                        <DataGridTemplateColumn Header="Role Group" Width="*">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding ListSecurityRoleGroup, 
                            RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValue="{Binding RoleGroupID,UpdateSourceTrigger=PropertyChanged}"
                                  DisplayMemberPath="Description" SelectedValuePath="ID" IsHitTestVisible="False">

                                    </ComboBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                            <DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding ListSecurityRoleGroup, 
                            RelativeSource={RelativeSource AncestorType=UserControl}}" 
                                  DisplayMemberPath="Description" SelectedValuePath="ID"  
                                              SelectedValue="{Binding RoleGroupID,UpdateSourceTrigger=PropertyChanged}"
                                  />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellEditingTemplate>
                        </DataGridTemplateColumn>

                    </DataGrid.Columns>
                </DataGrid>
            </DockPanel>
        </Grid>

You can also modify the combobox template to look like a textblock :

<ComboBox.Template>
        <ControlTemplate>
            <TextBlock Text="{Binding SelectedItem.Description,RelativeSource={RelativeSource Mode=TemplatedParent}}"></TextBlock>
        </ControlTemplate>
    </ComboBox.Template>

Upvotes: 2

Related Questions