0

I'm getting in such a mess with using a Combobox in a WPF grid.

I'm trying to implement a simple Contact form which allows the user to select a Salutation from a Combobox.

class Contact
{
    .. 
    public int SalutationID
    {
        get { return _salutationid;}
        set { _salutationid = value; }
    }
}

class Salutation
{
    public int ID
    {
        get { return _id;}
    }

    public string Description
    {
        get { return _description; }
    }
}

..

and in code

ObservableCollection<Contact> Contacts = GetContacts();
ObservableCollection<Salutation> Salutations = GetSalutations();


grid.ItemsSource = Contacts;
colSalutations.ItemsSource = Salutations;

The relevant XAML is:

 <DataGridComboBoxColumn x:Name="colSalutation" Header="Title" SelectedValueBinding="{Binding SalutationID}" SelectedValuePath="ID" DisplayMemberPath="Description" />

I only get an entry in the Salutation column for the last entry in the grid - but this row is invalid - it shouldn't be there (the entire row apart from this entry is blank). When I click to edit (on any row), a combo box appears with all the correct entries, but when I choose an item, it disappears and the field entry is blank.

I've looked at loads of examples and I appear to doing everything fine, but obviously not.

Can someone please show me where I'm going wrong? As you've probably realised, I'm new to WPF.

Thanks

I've also tried this:

<DataGridComboBoxColumn.ElementStyle>
    <Style TargetType="ComboBox">
        <Setter Property="IsDropDownOpen" Value="True" />
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Description}"></TextBlock>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
    <Style TargetType="ComboBox">
        <Setter Property="IsDropDownOpen" Value="True" />
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Description}"></TextBlock>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridComboBoxColumn.EditingElementStyle>

But this, while displaying a combobox (and still nothing for any other rows) together with the correct data, will not allow me to move off the current row, and I haven't a clue why!

Hopefully, to clarify matters, below is the FULL XAML :

        </Grid.Resources>

        <DockPanel>
            <DockPanel Name="ButtonPanel" DockPanel.Dock="Top" LastChildFill="false">
                <Button DockPanel.Dock="Left" Content="Save" x:Name="btnSave" Click="btnSave_Click" Height="28"/>
                <Button DockPanel.Dock="Right" Content="Cancel" x:Name="btnCancel" Click="btnCancel_Click" Height="28"/>
            </DockPanel>
            <ProgressBar DockPanel.Dock="Top" Name="pbLoading" Minimum="0" Maximum="1" Height="16" IsIndeterminate="True" Margin="0,0,0,16" />
            <DataGrid DockPanel.Dock="Top" x:Name="dgContacts"  AutoGenerateColumns="false" CellEditEnding="dgContacts_CellEditEnding" PreviewKeyDown="dgContacts_PreviewKeyDown" BeginningEdit="dgContacts_BeginningEdit" >

                <DataGrid.Columns>
                    <mui:DataGridTextColumn x:Name="colFirstName" Header="First Name"  Binding="{Binding fldForename}"/>
                    <mui:DataGridTextColumn x:Name="colLastName" Header="Last Name" Binding="{Binding fldSurname}" />
                    <mui:DataGridTextColumn x:Name="colEmailName" Header="Email" Binding="{Binding fldEmail}"/>
                    <mui:DataGridTextColumn x:Name="colPhoneNumber" Header="Telephone" Binding="{Binding fldPhoneNumber}" />

                    <mui:DataGridComboBoxColumn 
                        x:Name="colSalutation" Header="Title" 
                        SelectedItemBinding="{Binding SalutationID}" SelectedValuePath="ID"
                        DisplayMemberPath="Description">
                    </mui:DataGridComboBoxColumn> 

                    <mui:DataGridTextColumn x:Name="colAddressLine1" Header="Address 1" Binding="{Binding colAddressLine1}" />
                    <mui:DataGridTextColumn x:Name="colAddressLine2" Header="Address 2" Binding="{Binding colAddressLine2}" />
                    <mui:DataGridTextColumn x:Name="colAddressLine3" Header="Address 3" Binding="{Binding colAddressLine3}" />
                    <mui:DataGridTextColumn x:Name="colCity" Header="City" Binding="{Binding fldCity}" />
                    <mui:DataGridTextColumn x:Name="colCounty" Header="County" Binding="{Binding fldCounty}" />
                    <mui:DataGridTextColumn x:Name="colPostCode" Header="Postcode" Binding="{Binding fldPostCode}" />
                    <mui:DataGridTextColumn x:Name="colCountry" Header="Country" Binding="{Binding fldCountry}" />
                </DataGrid.Columns> 
            </DataGrid>
        </DockPanel>
    </Grid>
</UserControl>
Todd Bowles
  • 1,554
  • 15
  • 24
  • Unfortunately, when I implement the code you provided it works. You'll need to provide more context, or failing that, implement the minimal amount of code to reproduce and post it here. – Todd Bowles Jan 14 '14 at 11:38

1 Answers1

0

The setter in your ID propery is missing.

gomi42
  • 2,449
  • 1
  • 14
  • 9
  • Thanks, but this is not the problem. The ID belongs to the Salutation class bound to the items in the combo box. This is NOT supposed to change. What IS supposed to change is the SalutationID of the Contact class. – Muckers Mucker Jan 14 '14 at 11:34
  • So this WAS the problem? Its been marked as the answer with no additional information. Just curious. – Todd Bowles Jan 14 '14 at 21:38