1

I try to bind a list from a database to a DataGridComboBoxColumn for example I also use a ComboBox But I also not able to do it

I have tried some solutions I saw on this site but no one helps for example: DataGridComboBoxColumn binding to ObservableCollection

I did also the same as in here

https://www.youtube.com/watch?v=zvyQNuuTqks&ab_channel=IAmTimCorey

and still not work...

  <DataGridTemplateColumn x:Name="DaysTypeDescriptionColumn" Header="יום המשמרת">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=DaysType.Description}" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                            <DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding Path=DaysTypes,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=DaysType}">
                                        <ComboBox.ItemTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Path=Description}" />
                                            </DataTemplate>
                                        </ComboBox.ItemTemplate>
                                    </ComboBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellEditingTemplate>
                        </DataGridTemplateColumn>

I am using FodyWeavers.xml

<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
  <PropertyChanged />
</Weavers>

My Shifts class :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MilBatDBModels.Shifts
{
    public class ShiftsExt : INotifyPropertyChanged
    {
        public ShiftsExt(int id, int workerId, string workerName, Positions position, ShiftsType shiftsType, DaysType daysType)
        {
            Id = id;
            WorkerId = workerId;
            WorkerName = workerName;
            Position = position;
            ShiftsType = shiftsType;
            DaysType = daysType;
        }
        public ShiftsExt()
        {
            Id = -1;
            WorkerId = -1;
            WorkerName = "";
            Position = new Positions(-1,"",1);
            ShiftsType = new ShiftsType(-1,"");
            DaysType = new  DaysType(-1, "");
        }

        public int Id { get; set; }
        public int WorkerId { get; set; }
        public string WorkerName { get; set; }
        public Positions Position { get; set; }
        public ShiftsType ShiftsType { get; set; }
        public DaysType DaysType { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

my : ShiftsType class

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MilBatDBModels.Shifts
{
    public class ShiftsType : INotifyPropertyChanged
    {
        public ShiftsType(int code, string description)
        {
            Code = code;
            Description = description;
        }

        public int Code { get; set; }
        public string Description { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

My Code of the controls :

            <DataGrid.Resources>
                <Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Background" Value="#FF001389"/>
                    <Setter Property="Foreground" Value="#CDCCFF"/>

                </Style>

                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <Trigger Property="DataGridCell.IsSelected" Value="True">
                            <Setter Property="Background" Value="Yellow" />
                            <Setter Property="Foreground" Value="Purple" />
                        </Trigger>
                    </Style.Triggers>
                </Style>

            </DataGrid.Resources>

            <DataGrid.Columns>
                <DataGridComboBoxColumn MinWidth="150" x:Name="PositionDescriptionColumn" Header="תיאור עמדה"
                                        DisplayMemberPath="{Binding Description}" 
                                        SelectedItemBinding="{Binding Positions,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                                        ItemsSource="{Binding Positions}">
                    <DataGridComboBoxColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                        </Style>
                    </DataGridComboBoxColumn.HeaderStyle>
                </DataGridComboBoxColumn>

                <DataGridComboBoxColumn MinWidth="150" x:Name="ShiftsTypeDescriptionColumn" Header="סוג המשמרת"  
                                        DisplayMemberPath="{Binding Description}" 
                                        SelectedItemBinding="{Binding ShiftsType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                                        ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ShiftsTypes}">
                    <DataGridComboBoxColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                        </Style>
                    </DataGridComboBoxColumn.HeaderStyle>
                </DataGridComboBoxColumn>

                <DataGridComboBoxColumn MinWidth="150" x:Name="DaysTypeDescriptionColumn" Header="יום המשמרת" 
                                        DisplayMemberPath="{Binding Description}" 
                                        SelectedItemBinding="{Binding DaysType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                                        ItemsSource="{Binding DaysTypes}">
                    <DataGridComboBoxColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                        </Style>
                    </DataGridComboBoxColumn.HeaderStyle>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>

        </DataGrid>

        <Button Content="עדכן את נתוני המשמרות" Command="{Binding UpdateAllShiftData}"></Button>

        <ComboBox DisplayMemberPath="{Binding Path=Description}" 
                  ItemsSource="{Binding Path=ShiftsTypes}"
                  SelectedItem="{Binding Path=Code}">
        </ComboBox>

My Code behind :

using Shifts.ModelView;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Shifts.Windows
{
    /// <summary>
    /// Interaction logic for ShiftsManager.xaml
    /// </summary>
    public partial class ShiftsManager : Window
    {
        public ShiftsManager()
        {
            InitializeComponent();
            DataContext = new ShiftsManagerMV(Close);
        }
    }
}

My Model:

using IniFiles;
using MilBatDBModels.Shifts;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static DataBaseManager.ShiftsDBManager;
using System;
using System.Windows.Input;
using MilBatDBModels.Common;

namespace Shifts.ModelView
{
    public class ShiftsManagerMV : INotifyPropertyChanged
    {
        private Action close;
        
        public ObservableCollection<ShiftsExt> Shifts { get; set; }
        public ObservableCollection<Worker> Workers { get; set; }

        public ObservableCollection<Positions> Positions { get; set; }
        public ObservableCollection<ShiftsType> ShiftsTypes { get; set; }
        public ObservableCollection<DaysType> DaysTypes { get; set; }

        


        private Worker workerRowSelectedRowItem;
        public Worker WorkerRowSelectedRowItem
        {
            get { return workerRowSelectedRowItem; }
            set {
                workerRowSelectedRowItem = value;
                
                    UpdateShifts();
                

            }
        }

        public ShiftsManagerMV(Action close)
        {
            Positions = new ObservableCollection<Positions> (ShiftsDBManagerInstance.GetPositions());
            ShiftsTypes = new ObservableCollection<ShiftsType>(ShiftsDBManagerInstance.GetShiftsTypes());
            DaysTypes = new ObservableCollection<DaysType>(ShiftsDBManagerInstance.GetDaysTypes());

            UpdateWorkers();
            this.close = close;
        }

        private void UpdateShifts()
        {
            if (workerRowSelectedRowItem != null)
            {
                Shifts = new ObservableCollection<ShiftsExt>
                    (ShiftsDBManagerInstance.SelectShifts(workerRowSelectedRowItem.ID));
            }
        }

        private void UpdateWorkers()
        {
            ShiftsIniManager s = new ShiftsIniManager();
            int _class = s.GetShiftsLocalClass();
            Workers = new ObservableCollection<Worker>
                (ShiftsDBManagerInstance.SelectWorkers(_class));
        }

        private string _WorkerNumberColumnFilter;
        public string WorkerNumberColumnFilter
        {
            get
            {
                return _WorkerNumberColumnFilter;
            }
            set
            {
                if (_WorkerNumberColumnFilter != value)
                {
                    _WorkerNumberColumnFilter = value;
                    RunFilterAction();
                }
            }
        }


        private string _WorkerNameColumnFilter;
        public string WorkerNameColumnFilter
        {
            get
            {
                return _WorkerNameColumnFilter;
            }
            set
            {
                if (_WorkerNameColumnFilter != value)
                {
                    _WorkerNameColumnFilter = value;
                    RunFilterAction();
                }
            }
        }

        private void RunFilterAction()
        {
            UpdateWorkers();
            List<Worker> wTemp = Workers.ToList();
            if (string.IsNullOrWhiteSpace(_WorkerNumberColumnFilter) == false)
            {
                wTemp = wTemp.
                    Where(w => w.WorkerNumber.ToLower().Contains(_WorkerNumberColumnFilter.ToLower())).ToList();
            }
            if (string.IsNullOrWhiteSpace(_WorkerNameColumnFilter) == false)
            {
                wTemp = wTemp.
                    Where(w => w.WorkerName.ToLower().Contains(_WorkerNameColumnFilter.ToLower())).ToList();
            }
            Workers = new ObservableCollection<Worker>(wTemp);
        }











        private string _PositionDescriptionColumnFilter;
        public string PositionDescriptionColumnFilter
        {
            get
            {
                return _PositionDescriptionColumnFilter;
            }
            set
            {
                if (_PositionDescriptionColumnFilter != value)
                {
                    _PositionDescriptionColumnFilter = value;
                    RunFilterShiftsAction();
                }
            }
        }


        private string _ShiftsTypeDescriptionColumnFilter;
        public string ShiftsTypeDescriptionColumnFilter
        {
            get
            {
                return _ShiftsTypeDescriptionColumnFilter;
            }
            set
            {
                if (_ShiftsTypeDescriptionColumnFilter != value)
                {
                    _ShiftsTypeDescriptionColumnFilter = value;
                    RunFilterShiftsAction();
                }
            }
        }

        private string _DaysTypeDescriptionColumnFilter;
        public string DaysTypeDescriptionColumnFilter
        {
            get
            {
                return _DaysTypeDescriptionColumnFilter;
            }
            set
            {
                if (_DaysTypeDescriptionColumnFilter != value)
                {
                    _DaysTypeDescriptionColumnFilter = value;
                    RunFilterShiftsAction();
                }
            }
        }

        private void RunFilterShiftsAction()
        {
            UpdateShifts();
            List<ShiftsExt> sTemp = Shifts.ToList();
            if (string.IsNullOrWhiteSpace(_PositionDescriptionColumnFilter) == false)
            {
                sTemp = sTemp.
                    Where(s => s.PositionDescription.ToString().ToLower().Contains(_PositionDescriptionColumnFilter.ToLower())).ToList();
            }
            if (string.IsNullOrWhiteSpace(_ShiftsTypeDescriptionColumnFilter) == false)
            {
                sTemp = sTemp.
                    Where(s => s.ShiftTypeDescription.ToLower().Contains(_ShiftsTypeDescriptionColumnFilter.ToLower())).ToList();
            }
            if (string.IsNullOrWhiteSpace(_DaysTypeDescriptionColumnFilter) == false)
            {
                sTemp = sTemp.
                    Where(s => s.DaysTypeDescription.ToLower().Contains(_DaysTypeDescriptionColumnFilter.ToLower())).ToList();
            }
            Shifts = new ObservableCollection<ShiftsExt>(sTemp);
        }

        

        private ICommand _UpdateAllShiftData;
        public ICommand UpdateAllShiftData
        {
            get
            {
                if (_UpdateAllShiftData == null)
                {
                    _UpdateAllShiftData = new RelayCommand((param) => UpdateAllShiftDataAction());
                }
                return _UpdateAllShiftData;
            }
        }

        private void UpdateAllShiftDataAction()
        {
            //ShiftsIniManager s = new ShiftsIniManager();
            //for (int i = 0; i < Workers.Count(); i++)
            //{
            //    Workers[i].ClassCode = s.GetShiftsLocalClass();
            //}
            //List<Worker> wTemp = Workers.ToList();
            //wTemp.RemoveAll(w => w.WorkerName == null);
            //ShiftsDBManagerInstance.UpdateWorkers(wTemp);
            //UpdateWorkers();
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

Picture of my relevant List : enter image description here

My ComboBox for example :

enter image description here

My List on DataGridComboBoxColumn

enter image description here

picture of Shifts list : enter image description here

I update my shifts class

hedbisker
  • 179
  • 1
  • 1
  • 15

0 Answers0