0

I have a datagrid with a datagridComboBoxColumn. The items source of the datagrid is a custom class called Products which has a property called Installer (also a custom class called Contact).

I want to bind the datagridComboBoxColumn itemsSource to all the Contacts, and the selected value of the comboBox to the Installer. This is not working, could anyone please give me a hand? Thanks in advance

It would be much appreciated. I have seen other similar posts (like this one or this one ) but it's not exactly the same situation.

My xaml code:

 <DataGrid x:Name="productsList" AutoGenerateColumns="False" IsReadOnly="True" CanUserResizeRows="False"
              CanUserResizeColumns="True" ColumnWidth="*" GridLinesVisibility="None">
                <DataGrid.Columns>

                    <DataGridTextColumn Header="Ref" 
                                    Binding="{Binding Ref}" 
                                    />
                    <DataGridTextColumn Header="Product" 
                                    Binding="{Binding Product}" 
                                    />
                    <DataGridComboBoxColumn Header="Installer"  SelectedItemBinding="{Binding Installer, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Contacts}"/>

                </DataGrid.Columns>
 </DataGrid>

My code-behind:

 public partial class CatalogPage : Page
{
    ObservableCollection<CatalogProduct> mProductList = new ObservableCollection<CatalogProduct>();

    public ObservableCollection<Contact> Contacts
    {
        get
        {
            return Parent.mContactsPage.GetContacts();
        }
    }

    private LocalConfigurationPage Parent { get; set; }
    public CatalogPage(LocalConfigurationPage localConfigurationPage)
    {
        InitializeComponent();

        Parent = localConfigurationPage;

        productsList.ItemsSource = mProductList;


    }
}

This is the CatalogProduct class:

 public class CatalogProduct
{
    public string Ref { get; set; }
    public string Product { get; set; }
    public Contact Installer { get; set; }
}
Community
  • 1
  • 1
chincheta73
  • 187
  • 1
  • 22

1 Answers1

0

Couple of things you have done wrong here.

  1. Contacts is present in CatalogPage so, {Binding Contacts} wont work. This is because DataContext of a DataGridRow is the Item shown for that row. For your row, it would be CatalogProduct, and there is no Contacts there.

    Instead you have to do this :

    ItemsSource="{Binding DataContext.Contacts, RelativeSource={RelativeSource AncestorType=DataGrid}}

  2. Secondly, there are known issues with DataGridComboBoxColumn, so always use this :

        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox SelectedItem="{Binding Installer, UpdateSourceTrigger=PropertyChanged}}" ItemsSource="{Binding DataContext.Contacts, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    
  3. And finally, if you want to update your ComboBoxColumn with Installer value, implement change notification for Installer, and set Mode=TwoWay for SelectedItem. Otherwise, right now it will work Combobox -> Installer and not vice versa.

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • Thanks! I have done the changes you said and still doesn´t work. There is an improvement tho, now I can see the combobox, but it is empty. I think I am missing something obvious... I have set a breakpoint in the get statement of the Contacts property, but it doesn´t break there. I was expecting to break when the combobox ask for the Contacts. Usually I do this with code-behind instead of bindings, but here, if I name the ComboBox, I cannot access to it from code-behind, that´s why I am a bit lost here, sorry. Thanks for your help @AnjumSKhan – chincheta73 Aug 04 '16 at 12:24
  • @chincheta73 Your Contacts property is private, make it public. – AnjumSKhan Aug 04 '16 at 12:39
  • I already tried that before but didn´t update my post sorry. Now my post is updated, the Contacts property is public, and nothing changed. – chincheta73 Aug 04 '16 at 12:43
  • do you mean changing the DataGridTemplateColumn to a DataGridComboBoxColumn again and set the ItemsSource to the value you proposed? I already tried that and still doesn´t work. in the grid it doesn´t even appear a comboBox. – chincheta73 Aug 05 '16 at 12:46