views:

90

answers:

1

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>
A: 

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.

Goblin