You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.8 KiB
52 lines
1.8 KiB
using System;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
|
|
namespace DyeingComputer.UserClass
|
|
{
|
|
/// <summary>
|
|
/// 实时更新datagrid
|
|
/// 调用示例:DataGridHelper.SetRealTimeCommit(Grid, true); Grid为对象控件名
|
|
/// </summary>
|
|
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<DataGridCellEditEndingEventArgs> 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;
|
|
}
|
|
}
|
|
}
|
|
|