0
votes

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; }
}
1
apply updatesourcetrigger too. - AnjumSKhan
Sorry @AnjumSKhan, could you specify a bit more detailed? Where shall I apply this? As a property to the DataGrid? - chincheta73
... to comboboxcolumn in your selecteditem binding - AnjumSKhan
Thanks @AnjumSKhan. I did it ( see my post updated) but nothing changed. In the data grid it doesn´t even appear the combobox. - chincheta73
where is Installer in your code ? - AnjumSKhan

1 Answers

0
votes

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.