C# WPF DataGrid行内编辑

DataGrid默认情况下就支持行内编辑,只不过比较简单,通常是显示用TextBlock,双击编辑时用TextBox,不过DataGrid支持模板自定义,编辑时使用其它控件,这样就可以实现更强大的功能。

1、数据

……
public class classPowerSwitchList
{
    public String SwitcID { set; get; }
    public string SwitchWeek { set; get; }
    public String PowerON { set; get; }
    public String PowerOFF { set; get; }
}
……
public List classPowerSwitchLists = new List();
……
classPowerSwitchLists.Add(new classPowerSwitchList() { SwitcID = "1", SwitchWeek = "一", PowerON = "07:30", PowerOFF = "18:00" });
classPowerSwitchLists.Add(new classPowerSwitchList() { SwitcID = "2", SwitchWeek = "二", PowerON = "07:30", PowerOFF = "18:00" });
classPowerSwitchLists.Add(new classPowerSwitchList() { SwitcID = "3", SwitchWeek = "三", PowerON = "07:30", PowerOFF = "18:00" });
classPowerSwitchLists.Add(new classPowerSwitchList() { SwitcID = "4", SwitchWeek = "四", PowerON = "07:30", PowerOFF = "18:00" });
classPowerSwitchLists.Add(new classPowerSwitchList() { SwitcID = "5", SwitchWeek = "五", PowerON = "07:30", PowerOFF = "18:00" });
classPowerSwitchLists.Add(new classPowerSwitchList() { SwitcID = "6", SwitchWeek = "六", PowerON = "07:30", PowerOFF = "18:00" });
classPowerSwitchLists.Add(new classPowerSwitchList() { SwitcID = "7", SwitchWeek = "日", PowerON = "07:30", PowerOFF = "18:00" });
PowerSwitchList.ItemsSource = null;
PowerSwitchList.ItemsSource = classPowerSwitchLists;

2、xaml


    
        
    
    
        
    
    
        
    
    
    
    
        
            
                
                    
            
        
        
            
                
                    
            
        
        
            
                
                    
                
            
            
                
                    
                        
                        

3、处理代码

……
private void PowerSwitchList_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    Console.WriteLine("开始编辑");
}
private void PowerSwitchList_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    //如果点击了放弃按钮或ESC
    if (e.EditAction == DataGridEditAction.Cancel)
    {
        return;
    }
    //获取编辑模式显示的元素,因为使用了模板,因此这里是ContentPresenter类型
    ContentPresenter cp = e.EditingElement as ContentPresenter;
    if (cp != null && VisualTreeHelper.GetChildrenCount(cp) > 0)
    {
        //找到StackPanel
        StackPanel stackPanel = VisualTreeHelper.GetChild(cp, 0) as StackPanel;
        //再找到编辑框
        TextBox textBox = VisualTreeHelper.GetChild(stackPanel, 0) as TextBox;
        //如果找到
        if (textBox != null)
        {
            //新值
            string newValue = textBox.Text;
            //获取表格对象
            var dataGrid = sender as DataGrid;
            //判断输入是否符合要求 00:00-23:59
            Regex regex = new Regex(@"([0-1][0-9]|2[0-3]):([0-5][0-9])");
            if (regex.IsMatch(newValue))
            {
                //从“:”两侧各取2个数字
                string[] modifyNewValue = newValue.Split(':');
                if (modifyNewValue[0].Length>2)
                {
                    modifyNewValue[0] = modifyNewValue[0].Substring(modifyNewValue[0].Length - 2, 2);
                }
                newValue = modifyNewValue[0] + ":" + modifyNewValue[1].Substring(0, 2);
                textBox.Text = newValue;
                //修改列表
                if (textBox.Name == "PowerON_Edit")//判断是编辑哪个字段
                {
                    classPowerSwitchLists[dataGrid.SelectedIndex].PowerON = newValue;
                }
                else
                {
                    classPowerSwitchLists[dataGrid.SelectedIndex].PowerOFF = newValue;
                }
                //保存到ini
                PowerSwitch_SaveDataGridToINI();
            }
            else
            {
                MessageBox.Show("输入错误,格式应为(“00:00”至“23:59”)","错误",MessageBoxButton.OK,MessageBoxImage.Error);
                //找到编辑前的值
                var OLD_textBox = cp.Content as classPowerSwitchList;
                //改为以前的数值
                if (textBox.Name == "PowerON_Edit")//判断是编辑哪个字段
                {
                    textBox.Text = OLD_textBox.PowerON;
                }
                else
                {
                    textBox.Text = OLD_textBox.PowerOFF;
                }
                //重新进入编辑状态
                dataGrid.BeginEdit();
            }
            
        }
    }
    
}
private void btnCancelEdit_Click(object sender, RoutedEventArgs e)
{
    this.PowerSwitchList.CancelEdit();
}

private void btnConfirmcEdit_Click(object sender, RoutedEventArgs e)
{
    this.PowerSwitchList.CommitEdit();
}
……

最终效果:

C# WPF DataGrid行内编辑_第1张图片

 

C# WPF DataGrid行内编辑_第2张图片

 

你可能感兴趣的:(程序开发,C#,笔记,c#,wpf,开发语言)