So currently I can bind a list of objects to a combobox column from the code but I am having a hard time making it editable
See my code below
My Xaml
<DataGridComboBoxColumn x:Name="dgEmpcmbName" SelectedValueBinding="{Binding ID, UpdateSourceTrigger=LostFocus}" ClipboardContentBinding="{x:Null}" Header="Employee name" Width="Auto"/>
My cs file
dgEmpcmbName.ItemsSource = people.ToList();
dgEmpcmbName.SelectedValuePath = "ID";
dgEmpcmbName.DisplayMemberPath = "Name";
If anyone know how to make it editable or just add an event on SelectionChanged that would be awesome
Thanks
Edit:
Btw this is not a duplicate as I have seen the solutions with the datagrid template column but when I use a template column I can't bind the list from the code.. (I am not saying it is not possible I am saying I can't.. If you can please assist :))
Setup your column differently by created a template column and creating the ComboBox in the data template, with the bindings.
This was based off of this answer found here: Editabe DatagridComboBoxColumn in WPF using C#
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Employee name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox IsEditable="True" Text="{Binding People.Name}" ItemsSource="{Binding People}" SelectedValue="{Binding People.ID}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Edit: I think this is what you are looking for. In this example, I have a viewmodel which I bind to the XAML form.
ViewModel:
public class MainWindowViewModel
{
public MainWindowViewModel()
{
GridItems = new ObservableCollection<GridItem>() {
new GridItem() { Name = "Chef", PeopleId = 1 } };
PeopleItems = new ObservableCollection<PeopleItem>() {
new PeopleItem() { ID = 1, Name = "George" },
new PeopleItem() { ID = 2, Name = "Martha" } };
}
public ObservableCollection<GridItem> GridItems { get; set; }
public static ObservableCollection<PeopleItem> PeopleItems { get; set; }
}
public class GridItem
{
public string Name { get; set; }
public int PeopleId { get; set; }
}
public class PeopleItem
{
public int ID { get; set; }
public string Name { get; set; }
}
Then your XAML form:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
>
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding GridItems}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" />
<DataGridComboBoxColumn
ItemsSource="{Binding Source={x:Static local:MainWindowViewModel.PeopleItems }}"
DisplayMemberPath="Name"
SelectedValuePath="ID"
SelectedValueBinding="{Binding CompanyID}" />
</DataGrid.Columns>
</DataGrid>
Using your DataGridTemplateColumn, this is how I would do it. I found an example which I borrowed heavily from, See Answer from Slauma
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.