当WPF RichTextBox没有放入ToolBar时,它们的编辑命令会松散

时间:2010-08-20 20:12:58

标签: wpf formatting richtextbox datagridtemplatecolumn

DataGrid DataGridTemplateColumn 中使用以下代码,我的格式设置按钮已禁用(显示为灰色)。当放入工具栏时,格式设置按钮仅启用

当按钮放在工具栏中时,我不需要 CommandTarget 。因此,当我将它们放在ToolBar之外时,有些人会认为它必须与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>

1 个答案:

答案 0 :(得分:0)

ToolBar有FocusManager.IsFocusScope =“True”,默认为false。

只需将FocusManager.IsFocusScope =“True”放在包含按钮的面板内。

CommandTarget用于限制按钮,如果它们位于IsFocusScope =“True”面板中 - 例如如果你有两个RichTextBox,你只希望按钮可以在其中一个上工作。