Home > OS >  I need simple example to use combobox in datagrid(MVV
I need simple example to use combobox in datagrid(MVV

Time:10-12

I'm newbie in wpf, mvvm sort of things. I was trying to make wpf application like this. enter image description here

but what the trying is like this below.

enter image description here

I cannot find how to use combobox in datagrid. I googled about so much about but i coludn't find it.

Please simple example how to use combobox in datagrid.

CodePudding user response:

Try using the "DataGridComboBoxColumn" column.

Here an example: XAML

<DataGrid Name="myGrid" AutoGenerateColumns="False">
 <DataGrid.Columns>
  <DataGridTextColumn Header="Text" Binding="{Binding Name}">    
  </DataGridTextColumn>
<DataGridComboBoxColumn Header="Combobox" x:Name="ComboboxColumn" SelectedItemBinding="{Binding City}">
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>

XAML.cs

public partial class MainWindow : Window
{
   public MainWindow()
   {
string[] Cities = new string[]{ "MI", "MN", "LA" };
List<Person> Persons = new List<Person>();
Persons.Add(new Person { Name="Person 1", City= "MI" });
Persons.Add(new Person { Name = "Person 2", City = "MN" });

InitializeComponent();

ComboboxColumn.ItemsSource = Cities;
myGrid.ItemsSource = Persons;
}

   public class Person { 
   public string Name { get; set; }
   public string City { get; set; }
  }
}
  • Related