A project i'm working on requires a shortcut key to access a save dialog to dump the contents of a rich text box element into a file.
My keybinding and command binding are being done in XAML but the code behind is what i think is messing up.
My key and command binding is set up like so.
<KeyBinding Command="local:customCommands.saveFile" Key="S" Modifiers="Ctrl"/>
...
<CommandBinding Command="local:customCommands.saveFile" Executed="launchSaveDialog"/>
And this is the code behind for the WPF window
private void launchSaveDialog(object sender, ExecutedRoutedEventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Rich Text format(*.rtf)|*.rtf|";
dlg.DefaultExt = ".rtf";
dlg.OverwritePrompt = true;
if (dlg.ShowDialog() == true)
{
FileStream fileStream = new FileStream(dlg.FileName, FileMode.Create);
TextRange range = new TextRange(RTB.Document.ContentStart, RTB.Document.ContentEnd);
range.Save(fileStream, DataFormats.Rtf);
}
}
The save dialog doesn't show even though Ctrl+S is pressed. If it helps, the program runs full screen.
Also, is there a way of running a Winforms save dialog inside of the WPF app as a separate window
launchSaveDialog
by putting a breakpoint inlaunchSaveDialog
. – 15ee8f99-57ff-4f92-890c-b56153launchSaveDialog
, or you'd be seeing an exception about the invalid filter string. You need to remove the trailing pipe (|
) character. – 15ee8f99-57ff-4f92-890c-b56153