This is a migrated thread and some comments may be shown as answers.

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

2 Answers 42 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Dmitry
Top achievements
Rank 1
Dmitry asked on 22 Feb 2017, 09:28 AM

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 Answers, 1 is accepted

Sort by
0
Dmitry
Top achievements
Rank 1
answered on 22 Feb 2017, 10:12 AM

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.

0
Polya
Telerik team
answered on 24 Feb 2017, 12:28 PM
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.
Tags
Menu
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Dmitry
Top achievements
Rank 1
Polya
Telerik team
Share this question
or