Change Request: Copy ItemsSource binding on DataGridComboBoxColumn

Dec 9, 2008 at 10:53 AM
At the moment the DataGridComboBoxColumn will get the value for the ItemsSource property and set that on the generated ComboBox.
This method has the big disadvantage that it will only support StaticResources, since it wasn't possible for me to use StaticResouces I found the following workarround.

Original problem: http://www.codeplex.com/wpf/WorkItem/View.aspx?WorkItemId=8153

Inherited DataGridComboBoxColumn and copy the original ItemsSource binding to the generated ComboBox.
This will allow all bindings to work since the ComboBox is in the visual true (RelativeSource, ElementName,...) and the current StaticResources should also work.

My implementation (in C#):
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Windows.Controls;

    public class DataGridComboBoxColumnEx : DataGridComboBoxColumn
    {
        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            FrameworkElement element = base.GenerateEditingElement(cell, dataItem);
            CopyItemsSource(element);
            return element;
        }

        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            FrameworkElement element = base.GenerateElement(cell, dataItem);
            CopyItemsSource(element);
            return element;
        }

        private void CopyItemsSource(FrameworkElement element)
        {
            BindingOperations.SetBinding(element, ComboBox.ItemsSourceProperty, BindingOperations.GetBinding(this, ComboBox.ItemsSourceProperty));
        }
    }

The current ApplyColumnProperties method uses:
            DataGridHelper.SyncColumnProperty(this, comboBox, ComboBox.ItemsSourceProperty, ItemsSourceProperty);

Coordinator
Dec 18, 2008 at 6:57 PM
If you don't want to go through the exercise of subclassing, you can set the binding in the ElementStyle and EditingElementStyle.  See the example here, http://blogs.msdn.com/vinsibal/archive/2008/12/17/wpf-datagrid-dynamically-updating-datagridcomboboxcolumn.aspx.