I have a dataGrid with ItemSourse={Binding} , and I have a DataContext in my Window class, which I install from code before I open the window, and everything works fine. But, I had to replace the cells of one of the columns with the ComboBox , I did it like this:

 DataGridTemplateColumn column = new DataGridTemplateColumn(); //create the data template DataTemplate cellLayout = new DataTemplate(); cellLayout.DataType = typeof(DataGridTemplateColumn); //set up the ComboBox OrderedDictionary dictionary = DriverService.GetDriversDictionary(); FrameworkElementFactory cbFactory = new FrameworkElementFactory(typeof(ComboBox)); cbFactory.Name = "myComboFactory"; cbFactory.SetValue(ComboBox.ItemsSourceProperty, dictionary); cbFactory.SetValue(ComboBox.SelectedValuePathProperty, "Key"); cbFactory.SetValue(ComboBox.DisplayMemberPathProperty, "Value"); Binding bind = new Binding("/driverId"); bind.Converter = new DriverIdConverter(); bind.ConverterParameter = dictionary; bind.Source = DataContext; bind.Mode = BindingMode.TwoWay; cbFactory.SetBinding(ComboBox.SelectedValueProperty, bind); //set the visual tree of the data template cellLayout.VisualTree = cbFactory; //set the item template to be our shiny new data template column.CellTemplate = cellLayout; column.CellEditingTemplate = cellLayout; dataGrid.Columns[index] = column; 

And that's what happens, Binding("/driverId") always returns the first ItemSourse element, but this is the path to the current elements. So what am I doing wrong?

    1 answer 1

    And it was wrong that the context should not be specified, it was taken from the parent container. And obviously I had to specify the path, something did not work from the constructor. Here is the correct code:

     var bind = new Binding("driverId") { Path = new PropertyPath("driverId"), Mode = BindingMode.TwoWay }; 
    • Well, if you use wpf, then most of this when you can transfer to xaml (c) grumbler :) - iRumba