using System; using MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel.Params; using MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using DataGridRowEventArgs = ExtendedGrid.Microsoft.Windows.Controls.DataGridRowEventArgs; using ExDataGridRow = ExtendedGrid.Microsoft.Windows.Controls.DataGridRow; using MECF.Framework.UI.Client.RecipeEditorLib.DGExtension.CustomColumn; using System.Collections.Generic; using DataGridCellEditEndingEventArgs = ExtendedGrid.Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs; using MECF.Framework.UI.Client.CenterViews.Configs.Roles; using MECF.Framework.UI.Client.ClientBase; namespace MECF.Framework.UI.Client.RecipeEditorLib.DGExtension { /// /// Interaction logic for DataGridRecipe.xaml /// public partial class DataGridRecipe : UserControl { #region Constructors public DataGridRecipe() { InitializeComponent(); } #endregion #region Properties #endregion #region Dependency Properties #region RecipeData public static readonly DependencyProperty RecipeProperty = DependencyProperty.Register( nameof(Recipe), typeof(RecipeData), typeof(DataGridRecipe), new PropertyMetadata(default(RecipeData), RecipePropertyChangedCallback)); private static void RecipePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is DataGridRecipe dg) { if (e.NewValue is RecipeData recipe) { dg.dgCustom.ItemsSource = recipe.Steps; recipe.PropertyChanged += (sender, e) => { // 同步白名单编辑模式 if (e.PropertyName == nameof(RecipeData.IsAccessibleWhitelistEditMode)) { dg.dgCustom.IsAccessibleWhitelistEditMode = recipe.IsAccessibleWhitelistEditMode; } }; // 创建DataGrid列 dg.ClearColumns(); foreach (var col in recipe.Columns) dg.AddColumn(col); } else dg.dgCustom.ItemsSource = null; } } /// /// 设置或返回编辑器正在编辑的Recipe。 /// public RecipeData Recipe { get => (RecipeData)GetValue(RecipeProperty); set => SetValue(RecipeProperty, value); } #endregion #region FrozenColumnCount public static readonly DependencyProperty FrozenColumnCountProperty = DependencyProperty.Register( nameof(FrozenColumnCount), typeof(int), typeof(DataGridRecipe), new PropertyMetadata(default(int), FrozenColumnCountPropertyChangedCallback)); private static void FrozenColumnCountPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is DataGridRecipe dg) { if (e.NewValue is int count) dg.dgCustom.FrozenColumnCount = count; else dg.dgCustom.FrozenColumnCount = 0; } } /// /// 设置或返回编辑器冻结的列数。 /// public int FrozenColumnCount { get => (int)GetValue(FrozenColumnCountProperty); set => SetValue(FrozenColumnCountProperty, value); } #endregion #endregion #region Methods private void RowOnSelected(object sender, RoutedEventArgs e) { if (!(e.Source is ExDataGridRow dgr)) return; if (!(dgr.DataContext is RecipeStep recipeStep)) return; var param = recipeStep.FindParamByControlName(RecipeControlNames.STEP_NO); if (param is StepParam sp) { sp.IsChecked = true; } } private void RowOnUnselected(object sender, RoutedEventArgs e) { if (!(e.Source is ExDataGridRow dgr)) return; if (!(dgr.DataContext is RecipeStep recipeStep)) return; var param = recipeStep.FindParamByControlName(RecipeControlNames.STEP_NO); if (param is StepParam sp) { sp.IsChecked = false; } } private void ClearColumns() { dgCustom.Columns.Clear(); } /// /// 生成编辑器的列。 /// private void AddColumn(EditorDataGridTemplateColumnBase col) { // 设置ColumnHeaderTemplate和DataGridCellTemplate if (string.IsNullOrEmpty(col.StringCellTemplate) == false) col.CellTemplate = (DataTemplate)FindResource(col.StringCellTemplate); if (string.IsNullOrEmpty(col.StringCellEditingTemplate) == false) col.CellEditingTemplate = (DataTemplate)FindResource(col.StringCellEditingTemplate); if (string.IsNullOrEmpty(col.StringHeaderTemplate) == false) col.HeaderTemplate = (DataTemplate)FindResource(col.StringHeaderTemplate); // 通过Columns重建XDataGrid列 dgCustom.Columns.Add(col); } /// /// 滚动到并高亮指定的参数单元格。 /// /// public void FocusToParam(IParam param) { if (param == null) return; ResetHighlight(); var row = param.RowOwner; var col = param.ColumnOwner; if (row != null && col != null) { dgCustom.ScrollIntoView(param.Parent, col); param.Highlight(); } } /// /// 清楚当前Recipe中参数的高亮显示效果。 /// public void ResetHighlight() { Recipe?.Steps.ResetHighlight(); } #endregion #region Events private void DataGridCell_OnPreviewMouseDown(object sender, MouseButtonEventArgs e) { // 仅在编辑AccessPermissionWhitelist编辑模式下有效。 if (!Recipe.IsAccessibleWhitelistEditMode) return; if (sender is Border bdr && bdr.DataContext is Param param) { if (param.IsHighlighted) param.ResetHighlight(); else param.Highlight(); } } private void DgCustom_OnLoadingRow(object sender, DataGridRowEventArgs e) { e.Row.Selected += RowOnSelected; e.Row.Unselected += RowOnUnselected; if (sender is XDataGrid dg && e.Row.DataContext is RecipeStep step) { for (var i = 0; i < step.Count; i++) { step[i].RowOwner = e.Row; step[i].ColumnOwner = dg.Columns[i]; } } } private void DgCustom_OnUnloadingRow(object sender, DataGridRowEventArgs e) { e.Row.Selected -= RowOnSelected; e.Row.Unselected -= RowOnUnselected; } private void DgCustom_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { if (e.Row.Item is RecipeStep step) { step.CalculateGasFlow(); step.Parent.Validate(); } } private void Expander_OnExpanded(object sender, RoutedEventArgs e) { if (sender is Expander expander && expander.DataContext is ExpanderColumn col) { foreach (var subCol in col.ChildColumns) { subCol.UpdateVisibility(); } } } private void Expander_OnCollapsed(object sender, RoutedEventArgs e) { if (sender is Expander expander && expander.DataContext is ExpanderColumn col) { foreach (var subCol in col.ChildColumns) { subCol.Visibility = Visibility.Collapsed; } } } #endregion } }