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;
                    }
                }
            }
        }


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

 

posted @ 2013-09-06 15:16  gavin.huang  阅读(217)  评论(0编辑  收藏  举报