当 WPF RichTextBox 的 EditingCommands 未放入工具栏中时,它们会失去绑定

发布于 2024-09-15 07:40:41 字数 1662 浏览 7 评论 0原文

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

当按钮放入工具栏中时,我不需要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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩耳倾听 2024-09-22 07:40:41

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文