Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a combobox inside wpf datagrid?

How to bind (Itemssource and selected item) of a combobox inside wpf datagrid? I am using MVVM pattern. I have tried

<DataGridTemplateColumn Header="Example 9">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding PartIds, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding PartId, UpdateSourceTrigger=PropertyChanged}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

and

   <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding MyCars}" HorizontalAlignment="Left">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Model" Binding="{Binding Model}"/>
                    <DataGridTextColumn Header="Registration" Binding="{Binding Registration}"/>

                    <DataGridTemplateColumn Header="Example 12">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding CarParts, RelativeSource={RelativeSource AncestorType=Window}}" DisplayMemberPath="PartName" SelectedValuePath="PartID"  SelectedValue="{Binding PartId, UpdateSourceTrigger=PropertyChanged}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTextColumn Header="Selected" Binding="{Binding PartId}"/>

                </DataGrid.Columns>
            </DataGrid>

Properties used for data-binding

#region DataGrid List<String> Example

        public ObservableCollection<MyCar> MyCars { get; set; }
        public List<string> PartIds { get; set; } 

        #endregion

        #region DataGrid List<Class> Example

        public List<CarPart> CarParts { get; set; }

        #endregion

Reference: http://code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82

like image 310
user2330678 Avatar asked Feb 15 '23 08:02

user2330678


1 Answers

I've tried so many options but the easiest option i found is to generate load event of that combo box & Bind it with the list or data table.

e.g. In Xaml

 <DataGridTemplateColumn.CellEditingTemplate>
       <DataTemplate>
                  <ComboBox x:Name="cmbPayee" Loaded="cmbPayee_Loaded" Text="{Binding PayeeName, NotifyOnSourceUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValue ="{Binding PayeeID, NotifyOnSourceUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  DisplayMemberPath = "Payee1" SelectedValuePath="PayeeID"/>
      </DataTemplate>

In .cs code

private void cmbPayee_Loaded(object sender, RoutedEventArgs e)
    {
        ComboBox cmb = (ComboBox)sender;
            var res = from k in db.Payees
                      select k;
            cmb.ItemsSource = res.ToList();
            cmb.DisplayMemberPath = "Payee1";
            cmb.SelectedValuePath = "PayeeID";

    }
like image 162
Pranjal Jain Avatar answered Feb 17 '23 17:02

Pranjal Jain