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

  • 相关阅读:
    数据分析人员常犯的五大错误以及预防方法
    SAS中的Order By - Proc Sort
    SAS中的Order By - Proc Sort
    安全数据分析理念的变化
    安全数据分析理念的变化
    spss如何选择需要的变量?
    更改VS2010的[默认开发语言]
    POJ 1273 Drainage Ditches (网络最大流)
    HLS图像处理系列——肤色检測
    并发问题:大数据量的訪问
  • 原文地址:https://www.cnblogs.com/gavinhuang/p/3305523.html
Copyright © 2011-2022 走看看