/////////////////////////////////////////////////////////////////////////////// //Copyright (c) 2016, 北京探索者软件公司 //All rights reserved. //文件名称: GridCellSingleClickEditBehavior.cs //文件描述: 窗格单击编辑行为 //创 建 者: xls //创建日期: 2017-1-11 //版 本 号:1.0.0.0 //////////////////////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Interactivity; namespace FWindSoft.Wpf.Behaviors { public class GridCellSingleClickEditBehavior : Behavior { protected override void OnAttached() { base.OnAttached(); this.AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown; } protected override void OnDetaching() { base.OnDetaching(); this.AssociatedObject.PreviewMouseRightButtonDown -= AssociatedObject_PreviewMouseLeftButtonDown; } private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { UIElement uiel = e.OriginalSource as UIElement; if (uiel == null) return; var cell = uiel.GetParentType(); if (cell == null) return; if (!cell.IsEditing && !cell.IsReadOnly) { if (!cell.IsFocused) cell.Focus(); DataGrid grid = cell.GetParentType(); if (grid != null) { if (grid.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!cell.IsSelected) { cell.IsSelected = true; } } else { DataGridRow row = cell.GetParentType(); if (row != null && !row.IsSelected) { row.IsSelected = true; } } } } } } }