Inheritance: System.Windows.Controls.DataGridTemplateColumn
Exemple #1
0
        private static object?GetCellItem(this DataGridColumn column, object?item)
        {
            if (column is null || item is null)
            {
                return(null);
            }

            var descriptor = (PropertyDescriptor)column.GetValue(PropertyDescriptorProperty);

            if (descriptor != null)
            {
                return(descriptor.GetValue(item));
            }

            var binding = column switch
            {
                DataGridBoundColumn c => c.Binding as Binding,
                CellTemplateColumn c => c.Binding as Binding,
                _ => null,
            };

            if (binding is null)
            {
                return(null);
            }

            descriptor = TypeDescriptor.GetProperties(item)
                         .OfType <PropertyDescriptor>()
                         .SingleOrDefault(x => x.Name == binding.Path.Path);
            column.SetCurrentValue(PropertyDescriptorProperty, descriptor);

            return(descriptor?.GetValue(item));
        }
Exemple #2
0
        private static void SetCellTemplateProperty(DataGrid dataGrid, DependencyPropertyChangedEventArgs e, bool editingTemplate)
        {
            if (e.OldValue != null && e.NewValue == null)
            {
                for (var i = 0; i < dataGrid.Columns.Count; ++i)
                {
                    if (dataGrid.Columns[i] is CellTemplateColumn col)
                    {
                        var temp = editingTemplate ?
                                   col.GetValue(DataGridTemplateColumn.CellTemplateProperty) :
                                   col.GetValue(DataGridTemplateColumn.CellEditingTemplateProperty);
                        if (temp == null)
                        {
                            var tcol = new DataGridTextColumn {
                                Binding = col.Binding
                            };
                            dataGrid.Columns[i] = tcol;
                        }
                        else
                        {
                            SetCellTemplateProperty(dataGrid, (DataTemplate)e.NewValue, editingTemplate);
                        }
                    }
                }
            }
            else if (e.OldValue == null && e.NewValue != null)
            {
                for (var i = 0; i < dataGrid.Columns.Count; ++i)
                {
                    if (dataGrid.Columns[i] is DataGridTextColumn tcol)
                    {
                        var col = new CellTemplateColumn {
                            Binding = tcol.Binding
                        };
                        if (!editingTemplate)
                        {
                            col.CellTemplate = (DataTemplate)e.NewValue;
                        }
                        else
                        {
                            col.CellEditingTemplate = (DataTemplate)e.NewValue;
                        }

                        dataGrid.Columns[i] = col;
                    }
                }
            }
            else
            {
                SetCellTemplateProperty(dataGrid, (DataTemplate)e.NewValue, editingTemplate);
            }
        }
Exemple #3
0
            private static void OnDataGridAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
            {
                var col = new CellTemplateColumn
                {
                    CellTemplate                = ((DataGrid)sender).GetTemplate(),
                    CellEditingTemplate         = ((DataGrid)sender).GetEditingTemplate(),
                    CellTemplateSelector        = ((DataGrid)sender).GetTemplateSelector(),
                    CellEditingTemplateSelector = ((DataGrid)sender).GetEditingTemplateSelector(),
                };

                if ((e.Column as DataGridTextColumn)?.Binding is BindingBase binding)
                {
                    col.Binding = binding;
                    e.Column    = col;
                }
            }
Exemple #4
0
            private static void OnDataGridAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
            {
                var col = new CellTemplateColumn
                {
                    CellTemplate                = ((DataGrid)sender).GetTemplate(),
                    CellEditingTemplate         = ((DataGrid)sender).GetEditingTemplate(),
                    CellTemplateSelector        = ((DataGrid)sender).GetTemplateSelector(),
                    CellEditingTemplateSelector = ((DataGrid)sender).GetEditingTemplateSelector()
                };

                DataGridTextColumn tc = e.Column as DataGridTextColumn;

                if (tc?.Binding != null)
                {
                    col.Binding = tc.Binding;
                    e.Column    = col;
                }
            }
            private static void OnDataGridAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
            {
                var col = new CellTemplateColumn
                {
                    CellTemplate = ((DataGrid)sender).GetTemplate(),
                    CellEditingTemplate = ((DataGrid)sender).GetEditingTemplate(),
                    CellTemplateSelector = ((DataGrid)sender).GetTemplateSelector(),
                    CellEditingTemplateSelector = ((DataGrid)sender).GetEditingTemplateSelector()
                };

                DataGridTextColumn tc = e.Column as DataGridTextColumn;
                if (tc?.Binding != null)
                {
                    col.Binding = tc.Binding;
                    e.Column = col;
                }
            }
        private static void SetCellTemplateProperty(DataGrid dataGrid, DependencyPropertyChangedEventArgs e, bool editingtemplate)
        {
            if (e.OldValue != null && e.NewValue == null)
            {
                for (int i = 0; i < dataGrid.Columns.Count; ++i)
                {
                    var col = dataGrid.Columns[i] as CellTemplateColumn;
                    if (col != null)
                    {
                        var temp = editingtemplate ?
                                   col.GetValue(DataGridTemplateColumn.CellTemplateProperty) :
                                   col.GetValue(DataGridTemplateColumn.CellEditingTemplateProperty);
                        if (temp == null)
                        {
                            var tcol = new DataGridTextColumn();
                            tcol.Binding = col.Binding;
                            dataGrid.Columns[i] = tcol;
                        }
                        else
                        {
                            SetCellTemplateProperty(dataGrid, (DataTemplate)e.NewValue, editingtemplate);
                        }
                    }
                }
            }
            else if (e.OldValue == null && e.NewValue != null)
            {
                for (int i = 0; i < dataGrid.Columns.Count; ++i)
                {
                    var tcol = dataGrid.Columns[i] as DataGridTextColumn;
                    if (tcol != null)
                    {
                        var col = new CellTemplateColumn();
                        col.Binding = tcol.Binding;
                        if (!editingtemplate)
                        {
                            col.CellTemplate = (DataTemplate)e.NewValue;
                        }
                        else
                        {
                            col.CellEditingTemplate = (DataTemplate)e.NewValue;
                        }

                        dataGrid.Columns[i] = col;
                    }
                }
            }
            else
            {
                SetCellTemplateProperty(dataGrid, (DataTemplate)e.NewValue, editingtemplate);
            }
        }