繁体   English   中英

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

[英]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)的作用,因为它与问题无关。 整个功能都有效。 不起作用的是我的代码顶部的按钮禁用。 当我单击按钮时,由于我使用了Mouse.OverrideCursor = Cursors.Wait;因此 Cursor 更改为Wait Mouse.OverrideCursor = Cursors.Wait; . 奇怪的是,该按钮仅在CheckFileIsOpen方法CheckFileIsOpen的文件打开时才被禁用。 如果没有打开文件,则永远不会禁用该按钮。 而且我确信我只在班级顶部禁用按钮,并且只在最后启用它。

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

抱歉,我已经到了为这样一个“虚拟”思考发布问题的地步,因为对我来说这很明显。 但我不明白为什么按钮的 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.ScreenUpdating=false; ExcelApp.Calculation=manual; ExcelApp.DisplayAlerts=false; .

不要使ExcelApp.Visible=false;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM