Click here to Skip to main content
15,843,145 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a Collection of Values in an Array of Strings, or in a List or whatever, and what i want is to set this Collection of values as the Collection of items into all the ComboBoxes in a column as DataGridComboBox in WPF.

I think i don't have other way to access this Collection of Values and bind it equally to all the ComboBoxes (in XAML) beside the DataContext. But can i access the DataContext of a DataGrid from a DatGridComboBox Column (in XAML)? Can i do it? How?

How i set (in XAML) in DatagridComboBox to put this Collection of Items equally in all ComboBoxes? How i can achieve this?


Here is my XAML:
XML
<Grid Name="grid1">
    <DataGrid Name="dataGrid" AutoGenerateColumns="True">
        <DataGrid.Columns>
            <DataGridComboBoxColumn Header="Options" Width="100" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>



And here is my Code Behind:
C#
public MainWindow()
{
  InitializeComponent();
  string[] abc = {"10", "20", "30", "40", "50"};
  dataGrid.DataContext = abc;
}
Posted

1 solution

Hi Miguel,

I think it is not so hard. each element of data in your datasource (which should consist of one or more rows of data) should expose the list of combobox entries, like:
class Student
{
   private List<string> _grades = new List<string>() { "10", "20", "30", "40" }

   public string Name { get; set; }
   public List<string> Grades
   {
      get { return _grades; }
   }
}


In your initialization code, you can bind a list of persons to your grid:

List<Student> students = new List<Student>()
{
   new Student() { Name="Peter Petersen" },
   new Student() { Name="Arthur Arthurson" }
};

MyDataGrid.ItemsSource = students;


In your XAML, you can then use Grades as the binding for the ComboBox data.

Hope this will help you.

Regards, Perry
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900