I have a need for something similar. I have a custom user control as the cell editor. I need to bind to one column to control various behaviors of the control, and I need to bind to a second column to control the text that is displayed to the user. Assume that I'm binding to a data table with at least two columns, OrderNumber & OrderStatus. The Grid Columns have already be generated based on the content of the datatable by the point this code runs. The column that is bound to the Order status field is the field that the code runs against.
CellEditorBindingExtension ex = new CellEditorBindingExtension();
FrameworkElementFactory factory = new FrameworkElementFactory();
factory.Type =
typeof(StatusUserControl);
Binding b = new Binding();
b.Path =
new PropertyPath("OrderNumber");
factory.SetBinding(
StatusUserControl.DisplayValueProperty, b);
factory.SetBinding(
StatusUserControl.ValueProperty, ex.ProvideValue(null) as BindingBase);
DataTemplate template = new DataTemplate();
template.VisualTree = factory;
template.Seal();
orderStatusColumn.CellContentTemplate = template;
My problem is that the binding doesn't get set, and the following output is shown in the output window:
System.Windows.Data Error: 39 : BindingExpression path error: 'OrderNumber' property not found on 'object' ''String' (HashCode=-842352673)'. BindingExpression:Path=OrderNumber; DataItem='String' (HashCode=-842352673); target element is 'StatusUserControl' (Name=''); target property is 'DisplayValue' (type 'Object')
Because of the nature of this project, this has to be done in C#. Any ideas?