2

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.

1 Answer 1

2

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>
1
  • Thanks that worked; however now on the new row, I need to click three times before I get the drop down. First click puts a black border around the combo; second applies a blue highlight, third activates the drop down. I'll play around and see if I can fix this. Will also see what difference it makes looking like a text box - thanks.
    – andrew
    Jul 19, 2013 at 0:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.