using System;
using System.Windows;
using System.Windows.Controls;
namespace DyeingComputer.UserClass
{
    /// 
    /// 实时更新datagrid
    /// 调用示例:DataGridHelper.SetRealTimeCommit(Grid, true);  Grid为对象控件名
    /// 
    public static class DataGridHelper
    {
        public static void SetRealTimeCommit(DataGrid dataGrid, bool isRealTime)
        {
            dataGrid.SetValue(RealTimeCommitProperty, isRealTime);
        }
        public static bool GetRealTimeCommit(DataGrid dataGrid)
        {
            return (bool)dataGrid.GetValue(RealTimeCommitProperty);
        }
        public static readonly DependencyProperty RealTimeCommitProperty =
        DependencyProperty.RegisterAttached("RealTimeCommit", typeof(bool),
        typeof(DataGridHelper),
        new PropertyMetadata(false, RealTimeCommitCallBack));
        private static void RealTimeCommitCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var dg = d as DataGrid;
            if (dg == null)
                return;
            EventHandler ceHandler = delegate (object xx, DataGridCellEditEndingEventArgs yy)
            {
                var flag = GetRealTimeCommit(dg);
                if (!flag)
                    return;
                var cellContent = yy.Column.GetCellContent(yy.Row);
                if (cellContent != null && cellContent.BindingGroup != null)
                    cellContent.BindingGroup.CommitEdit();
            };
            dg.CellEditEnding += ceHandler;
            RoutedEventHandler eh = null;
            eh = (xx, yy) =>
            {
                dg.Unloaded -= eh;
                dg.CellEditEnding -= ceHandler;
            };
            dg.Unloaded += eh;
        }
    }
}