I am new to DataGrids. After much searching, I can not seem to do this.

My Goal: I want to be able to double-click on a single appointment contained within any cell. (The double-click will open an editing window).

The Problem: I want to use a DataTable as the ItemsSource for the DataGrid and would like to use a Style for each cell that will display the 4 contained appointment objects and allow each object to be selected. I can not get the correct binding for the ListView ItemsSource. (see xaml in step 3).

(Other then appearance, I am using a ListView to ultimately allow for the selection of each appointment object).

Any and ALL help would be most appreciated.

Thanks in Advance.

  1. Appointment Class:

    public class ScheduledAppointment : INotifyPropertyChanged
    {
        private readonly int rowNumber;
        private readonly int columnNumber;
        private readonly int appointmentNumber;
        private string _firstName;
        private string _mi;
        private string _lastName;
        private string _birthDate;
    
        public string firstName
        {
            get { return _firstName; }
            set { _firstName = value; RaisePropertyChanged(); }
        }
        public string lastName
        {
            get { return _lastName; }
            set { _lastName = value; RaisePropertyChanged(); }
        }
        public string mi
        {
            get { return _mi; }
            set { _mi = value; RaisePropertyChanged(); }
        }
        public string birthDate
        {
            get { return _birthDate; }
            set { _birthDate = value; RaisePropertyChanged(); }
        }
    
        public ScheduledAppointment(string firstName, string mi, string lastName, string birthDate)
        {
            this.firstName = firstName;
            this.mi = mi;
            this.lastName = lastName;
            this.birthDate = birthDate;
        }
    
        public ScheduledAppointment(int rowNumber, int columnNumber, int appointmentNumber)
        {
            this.rowNumber = rowNumber;
            this.columnNumber = columnNumber;
            this.appointmentNumber = appointmentNumber;
        }
    
        #region [INotifyPropertyChanged]
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
    
  2. Each Cell is to have 4 appointments:

    public class ScheduledAppointments : INotifyPropertyChanged
    {
        private DateTime _selectedDate;
        private DataView _dataView;
    
        public DataView DataView
        {
            get
            {
                return _dataView;
            }
        }
    
        public ScheduledAppointments(DateTime currentTime)
        {
            this.selectedDate = currentTime;
            MakeEmptySchedule(currentTime);
        }
    
        public DateTime selectedDate
        {
            get { return _selectedDate; }
            set { _selectedDate = value; RaisePropertyChanged(); }
        }
    
    
    
        public void MakeEmptySchedule(DateTime currentTime)
        {
    
            int rowCount = 9;           // 8:00 to 5:00 is 9 hours. 
            int columnCount = 4;        // 4 columns per row
    
    
            var t = new DataTable();
    
            // Add columns to table.
            t.Columns.Add(new DataColumn("0:00", typeof(Cell)));
            t.Columns.Add(new DataColumn("0:15", typeof(Cell)));
            t.Columns.Add(new DataColumn("0:30", typeof(Cell)));
            t.Columns.Add(new DataColumn("0:45", typeof(Cell)));
    
            // create the cells to be held by the table.
            var cells = new Cell[rowCount, t.Columns.Count];
    
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < columnCount; j++)
                {
                    cells[i,j]=new Cell(i, j);
                }
            }
    
            // Add data to DataTable
            for (var r = 0; r < rowCount; r++)
            {
                var newRow = t.NewRow();
                for (var c = 0; c < t.Columns.Count; c++)
                {
                    newRow[c] = cells[r, c];
                }
                t.Rows.Add(newRow);
            }
            _dataView = t.DefaultView;
        }
    
    
        #region [INotifyPropertyChanged]
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
    
  3. Use a DataGrid to display the Appointment objects--all appointment objects are to be displayed in the cell containing them.

    <DataGrid Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="4" Grid.RowSpan="6"
                ItemsSource="{Binding scheduledAppointments.DataView}" AutoGenerateColumns="False" >
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="0:00" Width="230" IsReadOnly="True">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="{x:Type DataGridColumnHeader}">
                                <Setter Property="HorizontalAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ListView ItemsSource="{Binding}">
                                    <ListView.View>
                                        <GridView>
                                            <GridViewColumn Header="First"     DisplayMemberBinding="{Binding rowNumber}" />
                                            <GridViewColumn Header="Last"      DisplayMemberBinding="{Binding columnNumber}"  />
                                            <GridViewColumn Header="BirthDate" DisplayMemberBinding="{Binding birthDate}" />
                                        </GridView>
                                    </ListView.View>
                                </ListView>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
                    <DataGridTemplateColumn Header="0:15" Width="230" IsReadOnly="True">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="{x:Type DataGridColumnHeader}">
                                <Setter Property="HorizontalAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ListView ItemsSource="{Binding}">
                                    <ListView.View>
                                        <GridView>
                                            <GridViewColumn Header="First"     DisplayMemberBinding="{Binding firstName}" />
                                            <GridViewColumn Header="Last"      DisplayMemberBinding="{Binding lastName}"  />
                                            <GridViewColumn Header="BirthDate" DisplayMemberBinding="{Binding birthDate}" />
                                        </GridView>
                                    </ListView.View>
                                </ListView>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
                    <DataGridTemplateColumn Header="0:30" Width="230" IsReadOnly="True" >
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="{x:Type DataGridColumnHeader}">
                                <Setter Property="HorizontalAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ListView ItemsSource="{Binding}">
                                    <ListView.View>
                                        <GridView>
                                            <GridViewColumn Header="First"     DisplayMemberBinding="{Binding firstName}" />
                                            <GridViewColumn Header="Last"      DisplayMemberBinding="{Binding lastName}"  />
                                            <GridViewColumn Header="BirthDate" DisplayMemberBinding="{Binding birthDate}" />
                                        </GridView>
                                    </ListView.View>
                                </ListView>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="0:45"  Width="230" IsReadOnly="True">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="{x:Type DataGridColumnHeader}">
                                <Setter Property="HorizontalAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ListView ItemsSource="{Binding}">
                                    <ListView.View>
                                        <GridView>
                                            <GridViewColumn Header="First"     DisplayMemberBinding="{Binding firstName}" />
                                            <GridViewColumn Header="Last"      DisplayMemberBinding="{Binding lastName}"  />
                                            <GridViewColumn Header="BirthDate" DisplayMemberBinding="{Binding birthDate}" />
                                        </GridView>
                                    </ListView.View>
                                </ListView>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
    
            </DataGrid>
    

0 Answers