Excel 即使 IsEnabled 属性 设置为 false,按钮也不会被禁用

Excel button is not disabled even when IsEnabled property is set to false

单击按钮时我希望禁用单击的按钮,并在执行过程后重新启用它。

虽然看起来很简单,但由于某种原因整个过程执行成功但按钮没有被禁用。它在特定情况下被禁用,而不是在按钮单击开始时被禁用。

下面是按钮的XAML代码

<Button 
    x:Name="PreviewReportButton"
    Click="PreviewExcelReportButton_Click"
    Background="{StaticResource ExportExcelButtonColor}"
    BorderBrush="{StaticResource ExportExcelButtonColor}"
    Focusable="False"
    IsEnabled="False"
    Width="80"
    Height="Auto"
    Margin="450,0,0,0"
    FontSize="9"
    FontWeight="DemiBold"
    HorizontalAlignment="Left"
    VerticalAlignment="Center"
    Grid.Column="3"
    Grid.Row="5"
    Cursor="Hand"
    VerticalContentAlignment="Center"
    HorizontalContentAlignment="Center">
    <TextBlock TextAlignment="Center">Button 1</TextBlock>
</Button>

c# VS 2019中的.cs文件代码

private void PreviewExcelReportButton_Click(object sender, RoutedEventArgs e)
{
    var btn_excel = (Button)sender;
    btn_excel.IsEnabled = false;
    //PreviewReportButton.IsEnabled = false; This is the same button as btn_excel (the button I want to click)

    Debug.WriteLine("Button must be disabled");

    try
    {
        //Check if file is open
        int IsMacroFileOpen = CheckFileIsOpen($@"{path}file_1.xlsm");
        int IsReportFileOpen = CheckFileIsOpen($@"{path}file_2.xlsm");
        int IsXLSXFileOpen = CheckFileIsOpen($@"{path}file_3.xlsx");

        Debug.WriteLine(IsMacroFileOpen);
        Debug.WriteLine(IsReportFileOpen);
        Debug.WriteLine(IsXLSXFileOpen);

        if (new[] { 1, 2 }.Contains(IsMacroFileOpen) || new[] { 1, 2 }.Contains(IsReportFileOpen) || new[] { 1, 2 }.Contains(IsXLSXFileOpen))
        {
            btn_excel.IsEnabled = true;
            return;
        }

        Mouse.OverrideCursor = Cursors.Wait;

        //Step 1: Create copy of standard report file
        CreateCopyReportServerNameDB($@"{path}file_1.xlsm");

        //Step 2: Run macro
        ExecuteExcelMacro($@"{path}file_2.xlsm");

        //Step 3: Open a copy of the xlsx updated file
        //Approach 1
        Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();

        ExcelApp.DisplayAlerts = false;
        ExcelApp.Visible = true;

        Microsoft.Office.Interop.Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Add($"{path}file_3.xlsx");
    }
    catch (Exception)
    {
        this.Effect = new BlurEffect();
        bool? Result = new CustomMessageBox($"Unable to execute Excel button.\nPlease contact application support", "Error produced", MessageType.Error, MessageButtons.Ok).ShowDialog();
        if (Result.Value)
        {
            this.Effect = null;
            return;
        }
        else
        {
            this.Effect = null;
            return;
        }
    }
    finally
    {
        Mouse.OverrideCursor = null;
        btn.IsEnabled = true;
    }
}

不要太在意每个方法(CreateCopyReportServerNameDB、ExecuteExcelMacro)的作用,因为它与问题无关。整个功能有效。不起作用的是我的代码顶部的按钮禁用。当我单击按钮时,光标变为 Wait,因为我使用 Mouse.OverrideCursor = Cursors.Wait;。奇怪的是,该按钮仅在文件打开时被禁用,该文件被 CheckFileIsOpen 方法捕获。如果没有文件打开,则该按钮永远不会被禁用。而且我确信我只在 class 的顶部禁用了按钮并且我只在最后重新启用它。

您还会注意到,如果 IsEnabled = false; 成功执行并且该行确实作为输出写入,我已经将 Debug.Writeline 写入。尽管该按钮永远不会被禁用,除非文件打开并被方法捕获 CheckFileIsOpen

抱歉,我已经达到 post 提出这样一个“虚拟”想法的问题,因为对我来说这是显而易见的。但我不明白为什么这会发生在按钮的 IsEnabled 上。如果这很简单,请道歉,但我无法弄清楚发生了什么。

问题好像出在我执行第二种方法的时候ExecuteExcelMacro 该方法的代码

public void ExecuteExcelMacro(string sourceFile)
{
    var destinationFile = @"file_1";
    Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();

    ExcelWorkBook = ExcelApp.Workbooks.Open(sourceFile);

    string macro = "ThisWorkbook.Run_Code";
    try
    {
        ExcelApp.Run(macro);
        Debug.WriteLine("Macro: " + macro + " executed successfully");
    }
    catch (Exception)
    {
        this.Effect = new BlurEffect();
        bool? Result = new CustomMessageBox($"Unable to Run Macro: {macro}", "Cannot execute Macro", MessageType.Error, MessageButtons.Ok).ShowDialog();
        if (Result.Value)
        {
            this.Effect = null;
            return;
        }
        else
        {
            this.Effect = null;
            return;
        }
        //Debug.WriteLine("Unable to Run Macro: " + macro + " Exception: " + ex.Message);
    }

    ExcelApp.DisplayAlerts = false;
    ExcelApp.Visible = false;

    ExcelWorkBook.SaveAs($@"{path}{destinationFile}", Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, Type.Missing);
    ExcelWorkBook.Close(0);
    ExcelApp.Quit();

    if (ExcelWorkBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook); }
    if (ExcelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); }
}

在 ExecuteExcelMacro() 方法中,创建 ExcelApp 实例后 使 ExcelApp.ScreenUpdating=false; ExcelApp.Calculation=manual; ExcelApp.DisplayAlerts=false;.

不发ExcelApp.Visible=false;

并且在 PreviewExcelReportButton_Click() 方法的最后一个块中,做 Mouse.OverrideCursor = Cursors.Arrow;