如何在c#中使用WPF对DataGrid中的Cell进行编辑-创新互联

如何在c#中使用WPF对DataGrid中的Cell进行编辑?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

成都网站制作、网站建设、外贸网站建设介绍好的网站是理念、设计和技术的结合。创新互联拥有的网站设计理念、多方位的设计风格、经验丰富的设计团队。提供PC端+手机端网站建设,用营销思维进行网站设计、采用先进技术开源代码、注重用户体验与SEO基础,将技术与创意整合到网站之中,以契合客户的方式做到创意性的视觉化效果。

1 MainWindow


    
        
            
                
            
        
        
            
        
        
    
    
        
            
                
                    
                        
                            
                        
                    
                
            
 
            
                
                    
                        
                            
                        
                    
                
            
 
        
    

在View部分主要是通过引用Xceed中的DataGridControl控件进行扩展的,这个里面主要是需要设置DataGridControl的View和DefaultCellEditor这个里面DefaultCellEditor是本文的重点,这个就是单元格Cell双击后进行编辑的主体,在这个里面我们需要指定CellEditor的EditTemplate,这里面需要匹配一个DataTemplate,这个里面是一个SmartCellEditor的子View,下面我们来看看这个SmartCellEditor里面是什么内容?

2 SmartCellEditor


    
        
        
        
        
    
 
    
        
        
 
 
        
        
 
        
        
 
 
        
        
 
 
        
        
 
 
        
        
 
 
        
        
 
    

在这个里面我们在一个StackPanel中放置了匹配各种数据类型的Template,并且每一个的Visibility都是由visConverter这个自定义的Converter来实现的,后面我们会分析这个Converter里面的内容,这些代码的整体思想就是每次这个StackPanel里面的Template都只有一个可以显示,其它的都是隐藏的,哪一个会显示是根据当前的数据类型决定的,每一个注释表示每一个类型的数据,比如我们如果定义的是Bool类型,那么当我们双击单元格Cell的时候会出现一个CheckBox供我们编辑,所以这个里面我们需要根据我们定义的数据类型来扩展对应的模板,当我们双击单元格的时候就会显示这个模板从而进行编辑数据。

3 MainWindowViewModel

这个里面是定义的MainWindow对应的DataContext,在这里面我们会初始化绑定到MainWindow中DataGridControl的ItemsSource,我们先来看看这个里面核心的代码并就其中的要点进行分析。

using DataGridCellDoubleClickDemo.Models;
using System;
using System.Collections.ObjectModel;
using System.Windows;
 
namespace DataGridCellDoubleClickDemo
{
    public class MainWindowViewModel : NotificationObject
    {
        public MainWindowViewModel(Xceed.Wpf.DataGrid.DataGridControl dataGridControl)
        {
            DataGridControl = dataGridControl;
            InitRecipeVariables();
        }
 
 
        #region Properties
        private ObservableCollection _RecipeVariables = new ObservableCollection();
 
        public ObservableCollection RecipeVariables
        {
            get { return _RecipeVariables; }
            set
            {
                if (value != _RecipeVariables)
                {
                    _RecipeVariables = value;
                    OnPropertyChanged(nameof(RecipeVariables));
                }
 
            }
        }
 
        public Xceed.Wpf.DataGrid.DataGridControl DataGridControl { get; set; }
 
        #endregion
 
        #region Private Methods
        private void InitRecipeVariables()
        {
            _RecipeVariables.Add(new RecipeControlVariable
            {
                ControlName = "Name",
                DisplayName = "Name",
                StepValues = new ObservableCollection
                {
                    new SmartCellViewModel
                    {
                        CellValue="Step",
                        ErrorInfo=null,
                        SmartCellData=new RecipeVariableItem
                        {
                             ControlName = "Name",
                             DisplayName = "Name",
                             VariableEditorType=RecipeVariableEditorType.TextInput
                        }
                    },
                    new SmartCellViewModel
                    {
                        CellValue="Step",
                        ErrorInfo=null,
                        SmartCellData=new RecipeVariableItem
                        {
                             ControlName = "Name",
                             DisplayName = "Name",
                             VariableEditorType=RecipeVariableEditorType.TextInput
                        }
                    }
                }
 
            });
            _RecipeVariables.Add(new RecipeControlVariable
            {
                ControlName = "Time",
                DisplayName = "Process Time(Sec)",
                StepValues = new ObservableCollection
                {
                    new SmartCellViewModel
                    {
                        CellValue="0",
                        ErrorInfo=null,
                        SmartCellData=new RecipeVariableItem
                        {
                             ControlName = "Time",
                             DisplayName = "Process Time(Sec)",
                             VariableEditorType=RecipeVariableEditorType.NumInput
                        }
                    },
                    new SmartCellViewModel
                    {
                        CellValue="0",
                        ErrorInfo=null,
                        SmartCellData=new RecipeVariableItem
                        {
                             ControlName = "Time",
                             DisplayName = "Process Time(Sec)",
                             VariableEditorType=RecipeVariableEditorType.NumInput
                        }
                    }
                }
 
            });
            _RecipeVariables.Add(new RecipeControlVariable
            {
                ControlName = "FrontChemical",
                DisplayName = "FrontChemical",
                StepValues = new ObservableCollection
                {
                    new SmartCellViewModel
                    {
                        CellValue="None",
                        ErrorInfo=null,
                        SmartCellData=new RecipeVariableItem
                        {
                             ControlName = "FrontChemical",
                             DisplayName = "FrontChemical",
                             VariableEditorType=RecipeVariableEditorType.ReadOnlySelection,
                             Selections=new ObservableCollection
                             {
                                 new SelectionItem
                                 {
                                     SelectionControlName="CHEM1",
                                     SelectionDisplayName="CHEM1",
                                 },
                                 new SelectionItem
                                 {
                                     SelectionControlName="N2",
                                     SelectionDisplayName="N2",
                                 },
                                 new SelectionItem
                                 {
                                     SelectionControlName="CDIW",
                                     SelectionDisplayName="CDIW",
                                 },
                                 new SelectionItem
                                 {
                                     SelectionControlName="",
                                     SelectionDisplayName="None",
                                 }
                             }
                        }
                    },
                    new SmartCellViewModel
                    {
                        CellValue="None",
                        ErrorInfo=null,
                        SmartCellData=new RecipeVariableItem
                        {
                             ControlName = "FrontChemical",
                             DisplayName = "FrontChemical",
                             VariableEditorType=RecipeVariableEditorType.ReadOnlySelection,
                             Selections=new ObservableCollection
                             {
                                 new SelectionItem
                                 {
                                     SelectionControlName="CHEM1",
                                     SelectionDisplayName="CHEM1",
                                 },
                                 new SelectionItem
                                 {
                                     SelectionControlName="N2",
                                     SelectionDisplayName="N2",
                                 },
                                 new SelectionItem
                                 {
                                     SelectionControlName="CDIW",
                                     SelectionDisplayName="CDIW",
                                 },
                                 new SelectionItem
                                 {
                                     SelectionControlName="",
                                     SelectionDisplayName="None",
                                 }
                             }
                        }
                    }
                }
 
            });
            _RecipeVariables.Add(new RecipeControlVariable
            {
                ControlName = "NozzleBindingSetting",
                DisplayName = "Nozzle Scan",
                StepValues = new ObservableCollection
                {
                    new SmartCellViewModel
                    {
                        CellValue="Default",
                        ErrorInfo=null,
                        SmartCellData=new RecipeVariableItem
                        {
                             ControlName = "NozzleBindingSetting",
                             DisplayName = "Nozzle Scan",
                             VariableEditorType=RecipeVariableEditorType.TextInput
                        }
                    },
                    new SmartCellViewModel
                    {
                        CellValue="Default",
                        ErrorInfo=null,
                        SmartCellData=new RecipeVariableItem
                        {
                             ControlName = "NozzleBindingSetting",
                             DisplayName = "Nozzle Scan",
                             VariableEditorType=RecipeVariableEditorType.TextInput
                        }
                    }
                }
 
            });
        }
        #endregion
 
        /// 
        /// reload datagrid content
        /// 
        public void RefreshDataGrid()
        {
            try
            {
                if (null == DataGridControl) return;
                //generate columns in Grid
                DataGridControl.CurrentColumn = null;
                if (DataGridControl.Columns.Count > 0)
                    DataGridControl.Columns.Clear();
 
                var template = (DataTemplate)this.DataGridControl.FindResource("CustomTemplate");
                var rowTemplate = (DataTemplate)this.DataGridControl.FindResource("RowHeadTemplate");
 
                DataGridControl.Columns.Add(new Xceed.Wpf.DataGrid.Column()
                {
                    Width = 140,
                    Title = "Name",
                    FieldName = ".",
                    CellContentTemplate = rowTemplate
                });
 
                var cellEditor = DataGridControl.DefaultCellEditors[typeof(SmartCellViewModel)];
 
                for (int index = 0; index < RecipeVariables[0].StepValues.Count; index++)
                {
                    int width = 1;
                    for (int n = 0; n < RecipeVariables.Count; n++)
                    {
                        string display = RecipeVariables[n].StepValues[index].Display;
                        if (!string.IsNullOrWhiteSpace(display))
                        {
                            int temp = display.Length * 7;
                            width = Math.Max(temp, width);
                        }
                    }
                    width = (int)(width * 1.1);
                    width = Math.Max(width, 80);
                    DataGridControl.Columns.Add(new Xceed.Wpf.DataGrid.Column()
                    {
 
                        Title = string.Format("Step {0}", index + 1),
                        FieldName = string.Format("StepValues[{0}]", index),
                        CellContentTemplate = template,
                        AllowSort = false,
                        Width = width,
                        MaxWidth = width * 2,
                        CellEditor = cellEditor
                    });
                }
 
            }
            catch (Exception ex)
            {
 
            }

在这个里面我们重点分析下RefreshDataGrid这个子函数,在我们的MainWindowViewModel中我们定义的RecipeVariables是最终绑定到MainWindow中定义的DataGridControl中的ItemsSource,是整个控件的数据源,由于我们这个DataGird的第一列和后面的Step列数据类型不同,所以我们的RefreshDataGrid函数中增加Column列的时候是分作两个部分,第一个部分是单独增加一列,后面的列是通过循环StepValues这个集合来动态进行增加的,代码中我们定义了多少个StepValue,那么后面就会有多少列,这个里面的重点是增加Column的时候FieldName的赋值,这个是十分关键的,这个关系到能够让每一列获取到正确的数据源,例如第一列赋值Filed= “.” 表示直接从当前绑定的数据源获取数据,另外后面的Step列的每一个FieldName是动态进行赋值的,赋值语句是:FieldName = string.Format("StepValues[{0}]", index),这个里面Index是一个动态值,这个是非常关键的一步,另外后面的Step列由于需要通过双击进行编辑所以每一个Column是需要赋值CellEditor对象的,另外这个ViewModel中的DataGridControl是通过构造函数进行赋值的,构造函数中的赋值就是MainWindow中定义的DataGridControl对象,这个在阅读代码时需要特别注意。

4 Models

Models主要是定义的数据集合,我们的代码中主要包括RecipeControlVariable和SmartViewModel这两个部分,这两个部分分别对应DataGridControl的数据源以及双击进行编辑的SmartCellEditor两个部分一一对应。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。


标题名称:如何在c#中使用WPF对DataGrid中的Cell进行编辑-创新互联
本文链接:http://cdiso.cn/article/dpcojd.html

其他资讯