WPF 命令键绑定的问题
Problems with WPF command key bindings
我正在处理的项目需要快捷键来访问保存对话框,以将富文本框元素的内容转储到文件中。
我的键绑定和命令绑定正在 XAML 中完成,但我认为背后的代码是搞砸了。
我的键和命令绑定是这样设置的。
<KeyBinding Command="local:customCommands.saveFile" Key="S" Modifiers="Ctrl"/>
...
<CommandBinding Command="local:customCommands.saveFile" Executed="launchSaveDialog"/>
这是 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);
}
}
即使按下 Ctrl+S,也不会显示保存对话框。
如果有帮助,程序将全屏运行。
此外,有没有办法 运行 WPF 应用程序内部的 Winforms 保存对话框作为单独的 window
这对我来说是第一次尝试(至少在 SaveFileDialog
引发有关过滤器字符串的异常之前)。我将 KeyBinding
放在 Window.InputBindings
中,将 CommandBinding
放在 Window.CommandBindings
中。
<Window
x:Class="Test3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test3"
xmlns:commands="clr-namespace:Commands"
Title="MainWindow"
Height="350"
Width="525">
<Window.InputBindings>
<KeyBinding Command="local:customCommands.saveFile" Key="S" Modifiers="Ctrl"/>
</Window.InputBindings>
<Window.CommandBindings>
<CommandBinding Command="local:customCommands.saveFile" Executed="launchSaveDialog"/>
</Window.CommandBindings>
<Grid>
<RichTextBox x:Name="RTB" />
</Grid>
</Window>
我定义customCommands
如下:
public static class customCommands
{
static customCommands()
{
saveFile = new RoutedCommand("saveFile", typeof(MainWindow));
}
public static RoutedCommand saveFile { get; private set; }
}
由于结尾的竖线字符,我得到了有关过滤器字符串的异常。好像把它当成分隔符,而不是终结符:
Provided filter string is not valid. Filter string should contain a description of the filter, followed by a vertical bar and the filter pattern. Must also separate multiple filter description and pattern pairs by a vertical bar. Must separate multiple extensions in a filter pattern with a semicolon. Example: "Image files (*.bmp, .jpg)|.bmp;.jpg|All files (.)|.*"
轻松修复:
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);
}
}
您可能在某处吃键盘输入,但您没有显示该代码,所以我只是把它扔在那里。
坚持 javaCase 名称相对无害,但对可读性影响不大。
我正在处理的项目需要快捷键来访问保存对话框,以将富文本框元素的内容转储到文件中。
我的键绑定和命令绑定正在 XAML 中完成,但我认为背后的代码是搞砸了。
我的键和命令绑定是这样设置的。
<KeyBinding Command="local:customCommands.saveFile" Key="S" Modifiers="Ctrl"/>
...
<CommandBinding Command="local:customCommands.saveFile" Executed="launchSaveDialog"/>
这是 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);
}
}
即使按下 Ctrl+S,也不会显示保存对话框。 如果有帮助,程序将全屏运行。
此外,有没有办法 运行 WPF 应用程序内部的 Winforms 保存对话框作为单独的 window
这对我来说是第一次尝试(至少在 SaveFileDialog
引发有关过滤器字符串的异常之前)。我将 KeyBinding
放在 Window.InputBindings
中,将 CommandBinding
放在 Window.CommandBindings
中。
<Window
x:Class="Test3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test3"
xmlns:commands="clr-namespace:Commands"
Title="MainWindow"
Height="350"
Width="525">
<Window.InputBindings>
<KeyBinding Command="local:customCommands.saveFile" Key="S" Modifiers="Ctrl"/>
</Window.InputBindings>
<Window.CommandBindings>
<CommandBinding Command="local:customCommands.saveFile" Executed="launchSaveDialog"/>
</Window.CommandBindings>
<Grid>
<RichTextBox x:Name="RTB" />
</Grid>
</Window>
我定义customCommands
如下:
public static class customCommands
{
static customCommands()
{
saveFile = new RoutedCommand("saveFile", typeof(MainWindow));
}
public static RoutedCommand saveFile { get; private set; }
}
由于结尾的竖线字符,我得到了有关过滤器字符串的异常。好像把它当成分隔符,而不是终结符:
Provided filter string is not valid. Filter string should contain a description of the filter, followed by a vertical bar and the filter pattern. Must also separate multiple filter description and pattern pairs by a vertical bar. Must separate multiple extensions in a filter pattern with a semicolon. Example: "Image files (*.bmp, .jpg)|.bmp;.jpg|All files (.)|.*"
轻松修复:
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);
}
}
您可能在某处吃键盘输入,但您没有显示该代码,所以我只是把它扔在那里。
坚持 javaCase 名称相对无害,但对可读性影响不大。