在 WPF 中,我尝试使用命令一次在多个文本框中设置所有内容。以下代码能够在切换按钮上获得一个命令来执行哪个文本框具有范围,但我似乎无法同时执行这两个命令。
<StackPanel>
<ToggleButton FocusManager.IsFocusScope="True" Command="EditingCommands.ToggleBold" Width="20" Height="20"></ToggleButton>
<RichTextBox Width="200" Height="200">
<RichTextBox.CommandBindings>
<CommandBinding
Command="EditingCommands.ToggleBold"
Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"/>
</RichTextBox.CommandBindings>
</RichTextBox>
<RichTextBox Width="200" Height="200">
<RichTextBox.CommandBindings>
<CommandBinding
Command="EditingCommands.ToggleBold"
Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"/>
</RichTextBox.CommandBindings>
</RichTextBox>
</StackPanel>
Private Sub CommandBinding_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
DirectCast(sender, RichTextBox).SelectAll()
DirectCast(sender, RichTextBox).Selection.ApplyPropertyValue(RichTextBox.FontWeightProperty, "Bold")
e.Handled = False
End Sub
Private Sub CommandBinding_CanExecute(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
e.CanExecute = True
End Sub
我正在尝试用命令做什么?我宁愿不必在 click_event 或类似的代码中直接引用每个文本框。
谢谢您的帮助!