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,
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
}
} |
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?
<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)
{
}
|
<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();
}
}
|
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.
Thanks,
Emanuel
<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();
}
}
|
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();
}
}