You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpreadsheetGear 9.2.44修改ActiveCell致撤销列表清空问题咨询

问题描述与求助

环境与版本信息

  • 使用版本:SpreadsheetGear 9.2.44
  • UI框架:WPF
  • 无法升级至9.2.59版本的原因:该版本更新日志中无ActiveCommandManager相关修复内容,且存在导致WPF程序崩溃的无效公式Bug

业务场景

当单元格满足特定条件时显示指定字体颜色;为提升编辑可读性,需要在开始编辑单元格时将文本重置为黑色,编辑结束后恢复原颜色。

当前解决方案与遇到的问题

我通过BeginCellEdit事件设置wbv.ActiveCell.Font.Color为黑色,在CellEditEnd事件中恢复原颜色,但发现执行wbv.ActiveCell.Font.Color赋值操作会清空wbv.ActiveCommandManager的撤销列表,导致编辑结束后仅能执行一次撤销操作。

现求助:

  • 是否有办法避免撤销列表被清空?
  • 若使用.ActiveCell时清空列表是预期行为,能否自行管理撤销列表?

复现代码

注:实际项目未使用反射,此处仅用于查看数据变化

MainWindow.xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ssgtesting"
        xmlns:sg="http://schemas.spreadsheetgear.com/controls/xaml" x:Class="ssgtesting.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <sg:WorkbookView CellBeginEdit="wbv_CellBeginEdit" Name="wbv"/>
        <Button Content="Undo" HorizontalAlignment="Left" Margin="203,40,0,0" VerticalAlignment="Top" Click="Button_Click" Width="265" Height="130"/>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Reflection;
using System.Windows;

namespace ssgtesting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                wbv.GetLock();
                var activeCommandManager = wbv.ActiveCommandManager;
                Type type = activeCommandManager.GetType();
                FieldInfo undoListField = type.GetField("_undoList", BindingFlags.NonPublic | BindingFlags.Instance);
                if (undoListField != null)
                {
                    var undoList = undoListField.GetValue(activeCommandManager);
                    // Set breakpoint here and check the count of the list
                    if (undoList != null) { }
                }
                wbv.ActiveCommandManager.Undo();
            }
            // This might throw depending on when you click undo because if you double click it since the list is only 1 then it cant undo twice.
            catch (Exception) { throw; }
            finally { wbv.ReleaseLock(); }
        }

        private void wbv_CellBeginEdit(object sender, SpreadsheetGear.Windows.Controls.CellBeginEditEventArgs e)
        {
            var activeCommandManager = wbv.ActiveCommandManager;
            Type type = activeCommandManager.GetType();
            FieldInfo undoListField = type.GetField("_undoList", BindingFlags.NonPublic | BindingFlags.Instance);
            if (undoListField != null)
            {
                var undoList = undoListField.GetValue(activeCommandManager);
                // Set breakpoint here and check the count of the list
                if (undoList != null) { }
            }

            // This nukes the undo list
            // Why does it do that? How can I avoid this?
            // Comment out this line to show that it clears the undo list
            wbv.ActiveCell.Font.Color = SpreadsheetGear.Color.FromArgb(0x00, 0x00, 0x00);

            activeCommandManager = wbv.ActiveCommandManager;
            type = activeCommandManager.GetType();
            undoListField = type.GetField("_undoList", BindingFlags.NonPublic | BindingFlags.Instance);
            if (undoListField != null)
            {
                var undoList = undoListField.GetValue(activeCommandManager);
                // Set breakpoint here and check the count of the list
                if (undoList != null) { }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Jason Adam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 15:42:35