当 WPF RichTextBox 的 EditingCommands 未放入工具栏中时,它们会失去绑定
在 DataGrid 的 DataGridTemplateColumn 中使用以下代码,我的格式设置按钮被禁用(呈灰色)。格式设置按钮仅在放入工具栏时才启用。
当按钮放入工具栏中时,我不需要CommandTarget。因此,当我将它们放在工具栏之外时,有些人可能会认为它必须与 CommandTarget 一起使用,但事实并非如此,为什么?
<Button Content="K" CommandTarget="{Binding ElementName=RTFBox}" Command="EditingCommands.ToggleItalic"/>
<Button Content="U" CommandTarget="{Binding ElementName=RTFBox}" Command="EditingCommands.ToggleUnderline" />
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Helper:RichTextBox VerticalScrollBarVisibility="Auto" x:Name="RTFBox" LostFocus="RTFBox_LostFocus" Text="{Binding Notes, UpdateSourceTrigger=PropertyChanged}" >
<Helper:RichTextBox.TextFormatter>
<Helper:RtfFormatter />
</Helper:RichTextBox.TextFormatter>
<RichTextBox.CommandBindings>
<CommandBinding Command="EditingCommands.ToggleUnderline"/>
<CommandBinding Command="EditingCommands.ToggleItalic"/>
</RichTextBox.CommandBindings>
</Helper:RichTextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Using the below code in a DataGridTemplateColumn of the DataGrid, my formatting buttons are disabled(grayed out). The formatting buttons are only enabled when they are put in a ToolBar.
When the buttons are put in a ToolBar I do not need the CommandTarget. So when I put them outside a ToolBar some could think it must work with CommandTarget, but it does not, WHY ?
<Button Content="K" CommandTarget="{Binding ElementName=RTFBox}" Command="EditingCommands.ToggleItalic"/>
<Button Content="U" CommandTarget="{Binding ElementName=RTFBox}" Command="EditingCommands.ToggleUnderline" />
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Helper:RichTextBox VerticalScrollBarVisibility="Auto" x:Name="RTFBox" LostFocus="RTFBox_LostFocus" Text="{Binding Notes, UpdateSourceTrigger=PropertyChanged}" >
<Helper:RichTextBox.TextFormatter>
<Helper:RtfFormatter />
</Helper:RichTextBox.TextFormatter>
<RichTextBox.CommandBindings>
<CommandBinding Command="EditingCommands.ToggleUnderline"/>
<CommandBinding Command="EditingCommands.ToggleItalic"/>
</RichTextBox.CommandBindings>
</Helper:RichTextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ToolBar 具有 FocusManager.IsFocusScope="True",默认情况下为 false。
只需将 FocusManager.IsFocusScope="True" 放入包含按钮的面板中即可。
CommandTarget 的作用是限制按钮位于 IsFocusScope="True" 面板中的情况 - 例如,如果您有两个 RichTextBox,并且您只希望按钮在其中之一上工作。
ToolBar has FocusManager.IsFocusScope="True" which is by default false.
Just put FocusManager.IsFocusScope="True" inside the panel that holds the buttons.
CommandTarget is to limit the buttons if they are in a IsFocusScope="True" panel - e.g. if you have two RichTextBoxes and you only want the buttons to work on one of them.