4

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

3

4 Answers 4

6

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";

    }
1

Try making your Lists observable collections. You need to make sure your properties are telling your UI when new objects are added to your collection and that is what an ObservableCollection does for you.

Also make sure your CarPart and MyCar class implement INotifyPropertyChanged.

Whether or not this is your problem depends on when exactly your properties are set.

1

Just a guess since I am not at my dev station, but try using ElementName to reference the window by name instead of relative source...

Something like:

ItemsSource="{Binding CarParts,ElementName=MyWindowName}"

and the add a Name="MyWindowName" to the window definition.

0

Just Use this os .cs side

DataContext = CarParts;

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.