Why does checkable RadMenuItem not react correctly on DialogResult value if System.Windows.Forms.FolderBrowserDialog is used in WPF MVVM application?

3 posts, 0 answers
  1. Yaroslav
    Yaroslav avatar
    153 posts
    Member since:
    Jan 2016

    Posted 22 Feb Link to this post

    Hello. Just on DialogResult, I'm not wrong! I use System.Windows.Forms.FolderBrowserDialog instance in my WPF MVVM application. Please see the following code from the 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;
       . . . . . . . . .
    }

    Where IsTurnOffExportToExcelSelected is:

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

    and IsTurnOnExportToExcelSelected is:

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

    Each one of these properties is binding source fore menu item in the View:

    <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>

    here the source code of TurnOnExportToExcelCommand. It turns on export to CSV-file of the values of real-time chart data-points:

    // 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;
            }
        }
    }

    The condition of if-statement above is satisfied if DialogResult is DialogResult.OK. I verify it to set breakpoint in debugger. But if I click 'Cancel' button in the dialog then the debugger doesn't not show next steps but IsChecked property of IsTurnOnExportToExcelSelected menu item is set to true (the status of IsChecked property of TurnOffExportToMSExcel menu item is not changed). Why is TurnOnExportToMSExcel boolean property set to true if DialogResult is Cancel?

  2. Yaroslav
    Yaroslav avatar
    153 posts
    Member since:
    Jan 2016

    Posted 22 Feb in reply to Yaroslav Link to this post

    I bag your pardon. The solution is very simple. I've added alternative condition to the if-statement in command method:

    else
    {
        this.IsTurnOffExportToExcelSelected = true;
        this.IsTurnOnExportToExcelSelected = false;
    }

    And  now RadMenuItem works perfectly. So please excuse me.

  3. Polya
    Admin
    Polya avatar
    238 posts

    Posted 24 Feb Link to this post

    Hello Yaroslav,

    We're happy to hear that you've found the solution for the issue and that the RadMenu performs as expected.

    Regards,
    Polya
    Telerik by Progress
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top