WPF VB.net - 是否可以对同一命令执行多个 CommandBinding?
在 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 或类似内容后面的代码中的每个文本框。
感谢您的帮助!
In WPF I'm trying to use commands to set all the content in multiple textboxes at once. The following code is able to get a command on the toggle button to execute for which ever textbox has scope but I can't seem to get the command to execute for both at the same time.
<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
Is what I am trying to do possible with commands? I would prefer not having to make direct references to each textbox in code behind inside a click_event or anything like.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将引用父容器,并在其子对象中查找指定类型,然后对子对象执行操作(只要它是您想要的类型)。
像这样的东西
I would reference the parent container, and look through it's children for a specified type, then perform your action on the child object providing it is the type you want.
Something like