zoukankan      html  css  js  c++  java
  • DataGrid研究笔记-3

    前面说了datagrid的焦点控制,以及校验,下面说说他的动画控制。

    1:datagrid的拖拽功能,很简单。

    先设置属性AllowDrop="True"

    然后注册两个事件

    productsDataGrid.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(productsDataGrid_PreviewMouseLeftButtonDown);
                productsDataGrid.Drop += new DragEventHandler(productsDataGrid_Drop);
    void productsDataGrid_Drop(object sender, DragEventArgs e)
            {
                if (rowIndex < 0)
                    return;
                var item = this.GetCurrentRowIndex(e.GetPosition);
                int index = item.Item1;
                if (index < 0)
                    return;
                if (index == rowIndex)
                    return;
                if (index == productsDataGrid.Items.Count - 1)
                {
                    MessageBox.Show("This row-index cannot be drop");
                    return;
                }
                ProductCollection productCollection = Resources["ProductList"] as ProductCollection;
                Product changedProduct = productCollection[rowIndex];
                productCollection.RemoveAt(rowIndex);
                productCollection.Insert(index, changedProduct);
            }
    
    void productsDataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                rowIndex = GetCurrentRowIndex(e.GetPosition).Item1;
                if (rowIndex < 0)
                    return;
                productsDataGrid.SelectedIndex = rowIndex;
                Product selectedEmp = productsDataGrid.Items[rowIndex] as Product;
                if (selectedEmp == null)
                    return;
                DragDropEffects dragdropeffects = DragDropEffects.Move;
                if (DragDrop.DoDragDrop(productsDataGrid, selectedEmp, dragdropeffects)
                                    != DragDropEffects.None)
                {
                    productsDataGrid.SelectedItem = selectedEmp;
                }
            }


    这样就可以拖动了,但是没有动作,动画,太呆板,下面加入拖动效果,以及选中行,字体变立体效果。

    private void DataGridRow_MouseEnter(object sender, MouseEventArgs e)
            {
                if (canBeginCartoon)
                {
                    IInputElement inputEl = productsDataGrid.InputHitTest(Mouse.GetPosition(productsDataGrid));
                    while (inputEl != productsDataGrid)
                    {
                        if (inputEl != null && inputEl is DataGridRow)
                        {
                            DataGridRow dr = inputEl as DataGridRow;
                            _preDataGridRow = dr;
                            StoryboardBegin(dr);
                            return;
                        }
                        else
                        {
                            if (inputEl == null)
                                return;
                            inputEl = VisualTreeHelper.GetParent(inputEl as DependencyObject) as IInputElement;
                        }
                    }
                }
                else
                {
                    foreach (var item in productsDataGrid.Items)
                    {
                        foreach (var column in productsDataGrid.Columns)
                        {
                            var myContent = column.GetCellContent(item) as UIElement;
                            myContent.Effect = null;
                        }
                    }
                    IInputElement inputEl = productsDataGrid.InputHitTest(Mouse.GetPosition(productsDataGrid));
                    while (inputEl != productsDataGrid)
                    {
                        if (inputEl != null && inputEl is DataGridRow)
                        {
                            DataGridRow dr = inputEl as DataGridRow;
                            foreach (var column in productsDataGrid.Columns)
                            {
                                var maContent = column.GetCellContent(dr) as UIElement;
                                DropShadowEffect effect = new DropShadowEffect();
                                effect.Opacity = 0.8;
                                maContent.Effect = effect;
                            }
                            return;
                        }
                        else
                        {
                            if (inputEl == null)
                                return;
                            inputEl = VisualTreeHelper.GetParent(inputEl as DependencyObject) as IInputElement;
                        }
                    }
                }
            }


    源码下载:http://files.cnblogs.com/gavinhuang/DataGridDropTest.rar

  • 相关阅读:
    c#数据绑定(3)——数据转化为信息
    c#数据绑定(2)——删除DataTable的数据
    C # 数据绑定(1)——将DataTabel的data添加ListView
    如何下载Chrome离线版EXE安装文件和MSI版安装文件
    Windows Installer (MSI) 详解 参数介绍
    7za.exe 命令行用法,参数介绍
    命令行启动Win7系统操作部分功能
    升级WordPress后开启友情链接管理模块
    如何将文件所有者改为TrustedInstaller
    开机自检时出现问题后会出现各种各样的英文短句解说
  • 原文地址:https://www.cnblogs.com/gavinhuang/p/3305523.html
Copyright © 2011-2022 走看看