Ribbon default open file path

Hello,

I have a SfSpreadsheet grid with SfSpreadsheetRibbon control inside a RibbonWindow.

Is there a way to specify a default folder path when open file dialog is chosen form the backstage?


Thanks,



7 Replies 1 reply marked as answer

BT Balamurugan Thirumalaikumar Syncfusion Team July 27, 2021 12:05 PM UTC

Hi Emanuel, 

Thank you for interesting in Syncfusion support. 

We have checked your query at our end. In order to achieve your requirement we suggest you to use the FileDialog.InitialDirectory Property which supports to display the default directory path. Please refer the following code snippet and Microsoft documentation. 

Code Snippet 
private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    const string directory = "D:\\"; 
    OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
    openFileDialog1.InitialDirectory = directory; 
    if (openFileDialog1.ShowDialog() == true) 
    { 
        //Do your customization here 
    } 
} 


Thanks & Regards, 
Balamurugan Thirumalaikumar  
 



EM Emanuel July 27, 2021 06:30 PM UTC

Hello,

Yes, I know how to use File Dialog.

The question was, how can I customize the way this dialog is fired when using the standard RibbonWindow.

My Xaml looks something like this:

  

<syncfusion:RibbonWindow

    x:Class="Views.SpreadSheetWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:syncfusion="http://schemas.syncfusion.com/wpf"

    Title="Data Management"

    d:DataContext="{d:DesignData SpreadSheetViewModel}"

    mc:Ignorable="d">


    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto" />

            <RowDefinition />

        </Grid.RowDefinitions>

        <syncfusion:SfSpreadsheetRibbon

            x:Name="Ribbon"

            Grid.Row="0"

            DataContext="{Binding ElementName=Spreadsheet, IsAsync=True}" />

        <syncfusion:SfSpreadsheet

            x:Name="Spreadsheet"

            Grid.Row="1"

            FormulaBarVisibility="Visible"

            Visibility="Visible" />

    </Grid>

</syncfusion:RibbonWindow>


What is the event for File Open form the backstage of the Ribbon Window?



VR Vijayalakshmi Roopkumar Syncfusion Team July 28, 2021 10:18 AM UTC

Hi Emanuel 
 
Thank you for your update. 
 
Query: The question was, how can I customize the way this dialog is fired when using the standard RibbonWindow. What is the event for File Open form the backstage of the Ribbon Window? 
 
From your query we could understand that you want to customize the file dialog from the backstage and you want to know which event get fired on opening the backstage in RibbonWindow. 
 
The BackstageOpening event of Ribbon is fired on opening the backstage as shown in following code: 
 
Code[XAML & c#] 
 
 
<syncfusion:Ribbon 
Name="mainRibbon" 
local:ViewModel.Ribbon="{Binding ElementName=mainRibbon}" BackStageOpening="mainRibbon_BackStageOpening" 
BackStageColor="{Binding ElementName=backColor, Path=Color, Mode=OneWay, Converter={StaticResource ColorToBrushConverter}}" 
BackStageHeader="File" 
IsBackStageVisible="True"> 
 
//Event to hook on backstageopening 
private void mainRibbon_BackStageOpening(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
 
} 
 
 
 
Also if you want to customize file dialog to be open from the Backstage , you can associate the respective file dialog opening code in BackStageCommandButton's command property as shown in the following code 
 
Code 
 
 
<syncfusion:BackStageCommandButton 
syncfusion:Ribbon.KeyTip="E" 
Command="{Binding OpenCommand}" 
Header="Open" 
Icon="Resources/Open32.png" /> 
 
 
 
 
/// <summary> 
/// Method which is used to execute the open command. 
/// </summary> 
/// <param name="parameter">Specifies the object type parameter.</param> 
private void OpenExecute(object parameter) 
{ 
OpenFileDialog openFile = new OpenFileDialog(); 
openFile.Filter = "FlowDocument Files (*.rtf)|*.rtf|All Files (*.*)|*.*"; 
if (openFile.ShowDialog().Value) 
{ 
string path = openFile.FileName; 
filePath = path; 
DocContent = File.ReadAllText(filePath); 
ribbon.HideBackStage(); 
} 
} 
 
 
 
Sample attached for your reference 
 
 
Please check this and let us know if it is helpful. 
 
Regards, 
Vijayalakshmi VR 



EM Emanuel July 29, 2021 06:11 PM UTC

Hello Vijayalakshmi ,


Thanks for the info, but that is not what I am interested in.

I do not want to customize anything related to the backstage.

Everything can remain standard as is. The only thing that I want to modify is when, after the backstage is opened and I click on Open File, then in the File Dialog that is opened I want to modify the default path.

So what I am interested in is the event for the open file action not the open backstage.

Capture.PNG

Thanks,

Emanuel



VR Vijayalakshmi Roopkumar Syncfusion Team July 30, 2021 07:05 AM UTC

Hi Emanuel 
 
Thank you for your update. 
 
From your query, we could understand that you want to operate the open file functionality in the highlighted open button in Backstage. For this you can achieve by using the Command property of BackstageCommandButton as shown in the following code. 
 
Code[XAML] 
  
  
<syncfusion:BackStageCommandButton  
syncfusion:Ribbon.KeyTip="E"  
Command="{Binding OpenCommand}"  
Header="Open"  
Icon="Resources/Open32.png" />  
  
  
  
C# 
  
/// <summary>  
/// Method which is used to execute the open command.  
/// </summary>  
/// <param name="parameter">Specifies the object type parameter.</param>  
private void OpenExecute(object parameter)  
{  
OpenFileDialog openFile = new OpenFileDialog();  
openFile.Filter = "FlowDocument Files (*.rtf)|*.rtf|All Files (*.*)|*.*";  
if (openFile.ShowDialog().Value)  
{  
string path = openFile.FileName;  
filePath = path;  
DocContent = File.ReadAllText(filePath);  
ribbon.HideBackStage();  
}  
}  
  
  
  
 
 
Sample for your reference: 
 
  
 
 
Please let us know if you need any further assistance on this. 
 
Regards, 
Vijayalakshmi VR 



EM Emanuel August 23, 2021 06:59 PM UTC

Hello,


Thank you for the solution, but it is not really what I was looking for. Binding a command in such a way breaks MVVM since the view model knows about the view. In the end, I extended the SfSpreadsheet and added a new dependency property for the default file path:


public class SfSpreadSheetExt:SfSpreadsheet

{

public SfSpreadSheetExt() : base()

{

Commands.FileOpen = new FileOpenCommandExt(this, "FileOpen", DefaultOpenFilePath);

}


public string DefaultOpenFilePath

{

get => (string)GetValue(DefaultOpenFilePathProperty);

set => SetValue(DefaultOpenFilePathProperty, value);

}


public static readonly DependencyProperty DefaultOpenFilePathProperty =

DependencyProperty.Register(nameof(DefaultOpenFilePath), typeof(string), typeof(SfSpreadSheetExt), new PropertyMetadata(OnDefaultOpenFilePathChanged));


private static void OnDefaultOpenFilePathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

{

if (d is not SfSpreadSheetExt spreadSheet) return;

((FileOpenCommandExt) spreadSheet.Commands.FileOpen).DefaultPath = (string) e.NewValue;

}

}


public class FileOpenCommandExt : FileOpenCommand

{

public string DefaultPath { get; set; }


public FileOpenCommandExt(SfSpreadsheet spreadsheetControl, string commandName, string defaultPath) : base(

spreadsheetControl, commandName)

{

DefaultPath = defaultPath;

}


protected override void OnExecute(object parameter)

{

var openFileDlg = new Microsoft.Win32.OpenFileDialog

{

Filter =

"Excel 97 to 2003 Workbook|*.xls|Excel Workbook(*.xlsx)|*.xlsx|CSV (Comma delimited)|*.csv|All Excel Files|*.xlsx;*.xlsm;*.xls;*.xlt;*.csv",

FilterIndex = 4,

InitialDirectory =

Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty, DefaultPath)

};


var result = openFileDlg.ShowDialog();

if (result is null or false) return;


var stream = openFileDlg.OpenFile();

SfSpreadsheet.Open(stream);


SfSpreadsheet.FocusActiveGrid();

}

}



VR Vijayalakshmi Roopkumar Syncfusion Team August 24, 2021 07:26 AM UTC

Hi Emanuel 
  
Thank you for your update. 
  
 Hope that your solution works fine at your end. Please let us know if you need any other assistance on this. 
  
Regards, 
Vijayalakshmi VR 


Marked as answer
Loader.
Up arrow icon