开发者

WPF VB.net - Can multiple CommandBindings be executed for the same command?

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 开发者_StackOverflow中文版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!


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
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜