真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

如何在c#中使用WPF對DataGrid中的Cell進行編輯-創(chuàng)新互聯(lián)

如何在c#中使用WPF對DataGrid中的Cell進行編輯?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)介紹好的網(wǎng)站是理念、設(shè)計和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)擁有的網(wǎng)站設(shè)計理念、多方位的設(shè)計風格、經(jīng)驗豐富的設(shè)計團隊。提供PC端+手機端網(wǎng)站建設(shè),用營銷思維進行網(wǎng)站設(shè)計、采用先進技術(shù)開源代碼、注重用戶體驗與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。

1 MainWindow


    
        
            
                
            
        
        
            
        
        
    
    
        
            
                
                    
                        
                            
                        
                    
                
            
 
            
                
                    
                        
                            
                        
                    
                
            
 
        
    

在View部分主要是通過引用Xceed中的DataGridControl控件進行擴展的,這個里面主要是需要設(shè)置DataGridControl的View和DefaultCellEditor這個里面DefaultCellEditor是本文的重點,這個就是單元格Cell雙擊后進行編輯的主體,在這個里面我們需要指定CellEditor的EditTemplate,這里面需要匹配一個DataTemplate,這個里面是一個SmartCellEditor的子View,下面我們來看看這個SmartCellEditor里面是什么內(nèi)容?

2 SmartCellEditor


    
        
        
        
        
    
 
    
        
        
 
 
        
        
 
        
        
 
 
        
        
 
 
        
        
 
 
        
        
 
 
        
        
 
    

在這個里面我們在一個StackPanel中放置了匹配各種數(shù)據(jù)類型的Template,并且每一個的Visibility都是由visConverter這個自定義的Converter來實現(xiàn)的,后面我們會分析這個Converter里面的內(nèi)容,這些代碼的整體思想就是每次這個StackPanel里面的Template都只有一個可以顯示,其它的都是隱藏的,哪一個會顯示是根據(jù)當前的數(shù)據(jù)類型決定的,每一個注釋表示每一個類型的數(shù)據(jù),比如我們?nèi)绻x的是Bool類型,那么當我們雙擊單元格Cell的時候會出現(xiàn)一個CheckBox供我們編輯,所以這個里面我們需要根據(jù)我們定義的數(shù)據(jù)類型來擴展對應(yīng)的模板,當我們雙擊單元格的時候就會顯示這個模板從而進行編輯數(shù)據(jù)。

3 MainWindowViewModel

這個里面是定義的MainWindow對應(yīng)的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這個子函數(shù),在我們的MainWindowViewModel中我們定義的RecipeVariables是最終綁定到MainWindow中定義的DataGridControl中的ItemsSource,是整個控件的數(shù)據(jù)源,由于我們這個DataGird的第一列和后面的Step列數(shù)據(jù)類型不同,所以我們的RefreshDataGrid函數(shù)中增加Column列的時候是分作兩個部分,第一個部分是單獨增加一列,后面的列是通過循環(huán)StepValues這個集合來動態(tài)進行增加的,代碼中我們定義了多少個StepValue,那么后面就會有多少列,這個里面的重點是增加Column的時候FieldName的賦值,這個是十分關(guān)鍵的,這個關(guān)系到能夠讓每一列獲取到正確的數(shù)據(jù)源,例如第一列賦值Filed= “.” 表示直接從當前綁定的數(shù)據(jù)源獲取數(shù)據(jù),另外后面的Step列的每一個FieldName是動態(tài)進行賦值的,賦值語句是:FieldName = string.Format("StepValues[{0}]", index),這個里面Index是一個動態(tài)值,這個是非常關(guān)鍵的一步,另外后面的Step列由于需要通過雙擊進行編輯所以每一個Column是需要賦值CellEditor對象的,另外這個ViewModel中的DataGridControl是通過構(gòu)造函數(shù)進行賦值的,構(gòu)造函數(shù)中的賦值就是MainWindow中定義的DataGridControl對象,這個在閱讀代碼時需要特別注意。

4 Models

Models主要是定義的數(shù)據(jù)集合,我們的代碼中主要包括RecipeControlVariable和SmartViewModel這兩個部分,這兩個部分分別對應(yīng)DataGridControl的數(shù)據(jù)源以及雙擊進行編輯的SmartCellEditor兩個部分一一對應(yīng)。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。


本文題目:如何在c#中使用WPF對DataGrid中的Cell進行編輯-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://weahome.cn/article/dpcojd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部