2011-07-25 56 views
0

在WPF中,我試圖使用命令一次設置多個文本框中的所有內容。下面的代碼能夠得到一個命令切換按鈕執行哪個文本框有範圍,但我似乎無法得到命令同時執行兩個。WPF VB.net - 可以爲同一個命令執行多個CommandBindings嗎?

<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或類似內容中。

感謝您的幫助!

回答

0

我會引用父容器,並通過它的子節點查找指定的類型,然後在子對象上執行您的操作,只要它是您想要的類型。

喜歡的東西

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

我沒有想到這一點。好想法! –

相關問題