WPF RichTextBox的编辑命令在未放入工具栏时会松开其绑定

WPF RichTextBox的编辑命令在未放入工具栏时会松开其绑定,wpf,formatting,richtextbox,datagridtemplatecolumn,Wpf,Formatting,Richtextbox,Datagridtemplatecolumn,使用DataGrid的DataGridTemplateColumn中的以下代码,我的格式设置按钮被禁用(灰显)。格式按钮只有在放入工具栏时才启用 当按钮放在工具栏中时我不需要命令目标。所以,当我把它们放在工具栏外时,有些人可能会认为它必须与CommandTarget一起工作,但事实并非如此,为什么 <Button Content="K" CommandTarget="{Binding ElementName=RTFBox}" Command="EditingCommands.ToggleI

使用DataGridDataGridTemplateColumn中的以下代码,我的格式设置按钮被禁用(灰显)。格式按钮只有在放入工具栏时才启用

当按钮放在工具栏中时我不需要命令目标。所以,当我把它们放在工具栏外时,有些人可能会认为它必须与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>

工具栏上有FocusManager.IsFocusScope=“True”,默认情况下为false

只需将FocusManager.IsFocusScope=“True”放在容纳按钮的面板内

CommandTarget用于限制位于IsFocusScope=“True”面板中的按钮-例如,如果您有两个RichTextBox,并且您只希望按钮在其中一个上工作