Cancel Open File Dialog in SpreadSheet Programatically

5 posts, 0 answers
  1. sai
    sai avatar
    10 posts
    Member since:
    Apr 2014

    Posted 25 Mar 2015 Link to this post

    Hi,
    Based on some condition in code behind(.cs file) I want to prevent or not prevent spreadsheet to open the file dialog.
    I'm using the following code in XAML
    <controls:GodRadRibbonBackstageItem Header="Open"
                                                CloseOnClick="True"
                                                Icon="{telerik:IconResource IconRelativePath=16/open.png, IconSources={StaticResource IconPaths}}"
                                                Command="{Binding Path=OpenFile.Command}"
                                                IsEnabled="{Binding Path=OpenFile.IsEnabled}"
                                                IsSelectable="False" />

    Thanks,
    sai
  2. Tanya
    Admin
    Tanya avatar
    405 posts

    Posted 30 Mar 2015 Link to this post

    Hello Sai,

    The DataContext property of the RadRibbonView is set to the RadSpreadsheet's command descriptors.  In the snippet below is demonstrated how to set the IsEnabled property of the OpenFile command:
    this.radSpreadsheet.CommandDescriptors.OpenFile.IsEnabled = false;

    However, I am not sure if I fully understand your scenario and if this suggestion does not fit your needs, please, let me know.

    Regards,
    Tanya
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. DevCraft banner
  4. sai
    sai avatar
    10 posts
    Member since:
    Apr 2014

    Posted 30 Mar 2015 in reply to Tanya Link to this post

    I'm not looking for this. But I implemented this. (The below code is working fine for me), Suggest me if I can make it better than this.
    I removed this Command "Command="{Binding Path=OpenFile.Command}"" from XAML
    And implemented click event for this in code behind
     var radRibbonBackstageItem = ribbonView.Backstage.Items.Cast<RadRibbonBackstageItem>().First(x => x.Header.ToString() == "Open");
                radRibbonBackstageItem.Click += radRibbonBackstageItem_Click;
     void radRibbonBackstageItem_Click(object sender, RoutedEventArgs e)
            {
                if(A)
                    OpenFileDialog();
            }


      private void OpenFileDialog()
            {
                var openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == true)
                {
                    try
                    {
                        var extension = Path.GetExtension(openFileDialog.File.Name);
                        using (Stream input = openFileDialog.File.OpenRead())
                        {
                            radSpreadsheet.Workbook = WorkbookFormatProvidersManager.Import(extension, input);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
  5. Tanya
    Admin
    Tanya avatar
    405 posts

    Posted 30 Mar 2015 Link to this post

    Hello Sai,

    The approach is appropriate and there is not much else to suggest. The only thing I noticed is the code for getting the RadRibbonBackstageItem in the code behind. You could set the Name property of the RadRibbonBackstageItem instead of checking its name:
    <telerik:RadRibbonBackstageItem Header="Open" Name="ribbonBackstageItemOpenFile"

    After doing this you can reach the item just by typing its name from the XAML:
    this.ribbonBackstageItemOpenFile.Click += ribbonBackstageItemOpenFile_Click;

    Hope this helps.

    Regards,
    Tanya
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  6. sai
    sai avatar
    10 posts
    Member since:
    Apr 2014

    Posted 01 Apr 2015 in reply to Tanya Link to this post

    That's great! Thanks a lot...
Back to Top
DevCraft banner