如果在此WPF应用程序中使用System.Windows.Forms.FolderBrowserDialog,为什么WPF应用程序对DialogResult值没有正确响应?

时间:2017-02-20 11:57:48

标签: wpf winforms mvvm folderbrowserdialog

我在WPF MVVM应用程序中使用System.Windows.Forms.FolderBrowserDialog实例。请参阅View Model中的以下代码:

. . . . . . . .
using System.Windows.Forms;
. . . . . . . .
FolderBrowserDialog dialog = new FolderBrowserDialog();
DialogResult res = dialog.ShowDialog();
if (res == DialogResult.OK)
{
   this.PathToExcelExportRepository = string.Copy(dialog.SelectedPath);
   this.IsTurnOffExportToExcelSelected = false;
   this.IsTurnOnExportToExcelSelected = true;
   . . . . . . . . .
}

IsTurnOffExportToExcelSelected的位置:

   public bool IsTurnOffExportToExcelSelected
   {
        get { return this._isTurnOffExportToExcelSelected; }
        set { this.SetProperty(ref this._isTurnOffExportToExcelSelected, value); }
   }

IsTurnOnExportToExcelSelected是:

public bool IsTurnOnExportToExcelSelected
{
   get { return this._isTurnOnExportToExcelSelected; }
   set { this.SetProperty(ref this._isTurnOnExportToExcelSelected, value); }
}

这些属性中的每一个都是视图中的绑定源前菜单项:

<telerik:RadContextMenu.ContextMenu>
   <telerik:RadContextMenu>
       <telerik:RadMenuItem Header="Turn on export to MS Excel" IsCheckable="True" IsChecked="{Binding IsTurnOnExportToExcelSelected, Mode=TwoWay}"
                                 Command="{Binding TurnOnExportToExcelCommand}"/>
       <telerik:RadMenuItem Header="Turn off export to MS Excel" IsCheckable="True" IsChecked="{Binding IsTurnOffExportToExcelSelected, Mode=TwoWay}"
                                 Command="{Binding TurnOffExportToExcelCommand}"/>
   </telerik:RadContextMenu>
</telerik:RadContextMenu.ContextMenu>

如果DialogResultDialogResult.OK,则满足上述if语句的条件。我验证它在调试器中设置断点。但是,如果我点击“取消”&#39;对话框中的按钮然后调试器不显示后续步骤,但IsChecked菜单项的IsTurnOnExportToExcelSelected属性设置为trueIsChecked属性的状态TurnOffExportToMSExcel菜单项未更改)。如果TurnOnExportToMSExcelDialogResult,为什么Cancel布尔属性设置为true?

wkl 建议我在此处显示TurnOnExportToExcelCommand的源代码。它打开导出到CSV文件的实时图表数据点的值:

// This line is in the View Model constructor.
this.TurnOnExportToExcelCommand = new DelegateCommand(this.turnOnExportToExcel);

// This is the command definition in the View Model.
public DelegateCommand TurnOnExportToExcelCommand { get; private set; }

    private void turnOnExportToExcel()
    {
        FolderBrowserDialog dialog = new FolderBrowserDialog();
        DialogResult res = dialog.ShowDialog();

        if (res == DialogResult.OK)
        {
            this.PathToExcelExportRepository = string.Copy(dialog.SelectedPath);

            this.IsTurnOffExportToExcelSelected = false;
            this.IsTurnOnExportToExcelSelected = true;
            // Define for which chart (absolute or relative) export to CSV-file is turned on.
            if (this.IsAbsoluteSplineChartSelected || this.IsAbsoluteBarChartSelected)
            {
                // Export of absolute chart data-point to CSV-file:
                Interlocked.CompareExchange(ref this._isAbsoluteChartDataBeingExported, 1, 0);
                Task.Factory.StartNew(this.peekAbsoluteDataForExportToCsv, TaskCreationOptions.LongRunning);
                Task.Factory.StartNew(this.exportToCsvAbsoluteChartPoints, TaskCreationOptions.LongRunning);
                // Export absolute chart data-point values is turned on.
                GlobalStaticMembers.ChartsExportToExcelStatus[0] = true;
            }
            else if (this.IsComparativeSplineChartSelected || this.IsComparativeBarChartSelected)
            {
                // Export of relative chart data-point to CSV-file:
                Interlocked.CompareExchange(ref this._isComparativeChartDataBeingExported, 1, 0);
                Task.Factory.StartNew(this.peekComparativeDataForExportToCsv, TaskCreationOptions.LongRunning);
                Task.Factory.StartNew(this.exportToCsvComparativeChartPoints, TaskCreationOptions.LongRunning);
                // Export relative chart data-point values is turned on.
                GlobalStaticMembers.ChartsExportToExcelStatus[1] = true;
            }
        }
    }

如果您需要,那么我将为您提供从此命令调用的方法源代码:peekAbsoluteDataForExportToCsv,exportToCsvAbsoluteChartPoints,peekComparativeDataForExportToCsv,exportToCsvComparativeChartPoints。

0 个答案:

没有答案