WPF VB.net - 是否可以对同一命令执行多个 CommandBinding?

发布于 2024-11-26 09:07:46 字数 1562 浏览 2 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(1

一口甜 2024-12-03 09:07:46

我将引用父容器,并在其子对象中查找指定类型,然后对子对象执行操作(只要它是您想要的类型)。

像这样的东西

foreach(var child in MyRootPanel.Children)
{
    if (child is RichTextBox)
    {
        // Process whatever
    }
}

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

foreach(var child in MyRootPanel.Children)
{
    if (child is RichTextBox)
    {
        // Process whatever
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文