0

I have this problem, and I appreciate if you help me with it.
I have a ViewModel with public ObservableCollection<IProfile> Profiles { get; set; } Property which fills a DataGrid.
Inside the DataGrid, there is a ComboBox Column:

     <DataGridComboBoxColumn 
                    Header="Header"
                    Width="*"
                    Visibility="{Binding SelectedType, Converter={Commons:ProfileVisibilityConverter}}"
                    ItemsSource="{Binding PotentialReinforces}"
                    SelectedValueBinding="{Binding SectionID, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"
                    DisplayMemberPath="Name"
                    SelectedValuePath="ReinforceID"/>

I cannot use a static ItemSource since each row's ComboBox source is different compared to other rows. Therefore, I need dynamic ItemSource
Inside the Profile Class I have defined the PotentialReinforces as below and it provides a suitable result for each object:

        public ObservableCollection<Section> PotentialReinforces
        {
            get
            {
                var Ids = this != null ? Database.Reinforces.Get.Where(x => x.BlongID == this.SectionID).Select(x => x.SectionID.Value) : new int[0];
                if (Ids.Count() > 0)
                {
                    return new ObservableCollection<Section>(Database.ReinforceSections.Get.Where(x => Ids.Contains(x.SectionID)));
                }
                else return null;
            }
        }

When I debug the code, each Profile object has the potential collection according to my design but the Combobox doesn't recognize the collection as its ItemSource.
Thank you for your help.

1 Answers1

0

I solved this problem using an alteration on DataGridComboBoxColumn which I learned from Joe on this page. This simply defines a new type of DataGridComboBoxColumn which solves the problem.

    public class DataGridComboBoxColumnWithBindingHack : 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));
        }
    }
 <Commons:DataGridComboBoxColumnWithBindingHack 
                    Header="Header"
                    Width="*"
                    Visibility="{Binding SelectedType, Converter={Commons:ProfileVisibilityConverter}}"
                    ItemsSource="{Binding PotentialReinforces}"
                    SelectedValueBinding="{Binding ReinforceID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                    DisplayMemberPath="Name"
                    SelectedValuePath="SectionID"/>