2010-07-26 46 views
1

我打算禁用和啓用TabControl外部的按鈕,就像當前選項卡更改時TabItem中的按鈕一樣。但是TabItem的CommandBindings似乎沒有影響視覺樹的「向上」。什麼正確的方法來做到這一點?WPF路由命令和綁定每個選項卡

有了這個XAML:

<Window x:Class="WpfApplication10.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApplication10" 
    Title="MainWindow" Height="350" Width="525"> 
<StackPanel> 
    <Button Content="MyCommand1" Command="local:MainWindow.MyCommand1" /> 
    <Button Content="MyCommand2" Command="local:MainWindow.MyCommand2" /> 
    <TabControl> 
     <TabItem Header="tabItem1" Name="tabItem1"> 
      <TabItem.CommandBindings> 
       <CommandBinding Command="local:MainWindow.MyCommand1" 
           Executed="ExecuteMyCommand" /> 
      </TabItem.CommandBindings> 
      <StackPanel> 
       <Button Content="MyCommand1" Command="local:MainWindow.MyCommand1" /> 
       <Button Content="MyCommand2" Command="local:MainWindow.MyCommand2" /> 
      </StackPanel> 
     </TabItem> 
     <TabItem Header="tabItem2" Name="tabItem2"> 
      <TabItem.CommandBindings> 
       <CommandBinding Command="local:MainWindow.MyCommand2" 
           Executed="ExecuteMyCommand"/> 
      </TabItem.CommandBindings> 
      <StackPanel> 
       <Button Content="MyCommand1" Command="local:MainWindow.MyCommand1" /> 
       <Button Content="MyCommand2" Command="local:MainWindow.MyCommand2" /> 
      </StackPanel> 
     </TabItem> 
    </TabControl> 
</StackPanel> 
</Window> 

與此代碼背後:

public static readonly RoutedUICommand MyCommand1 = new RoutedUICommand(); 
    public static readonly RoutedUICommand MyCommand2 = new RoutedUICommand(); 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 
    private void ExecuteMyCommand(object sender, ExecutedRoutedEventArgs e) 
    { 
     MessageBox.Show("Hello"); 
    } 

回答

1

MSFT給了我正確的答案在他們的WPF論壇,在這裏(http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bb3d1eb1-96fa-4fbc-beda-799613acb9f7

<StackPanel> 
    <StackPanel FocusManager.IsFocusScope="True"> 
     <Button Content="MyCommand1" Command="local:Window8.MyCommand1" /> 
     <Button Content="MyCommand2" Command="local:Window8.MyCommand2" /> 
    </StackPanel> 
    <TabControl> 
     <TabItem Header="tabItem1" Name="tabItem1"> 
      <TabItem.CommandBindings> 
       <CommandBinding Command="local:Window8.MyCommand1" Executed="ExecuteMyCommand" /> 
      </TabItem.CommandBindings> 
      <StackPanel> 
       <Button Content="MyCommand1" Command="local:Window8.MyCommand1" /> 
       <Button Content="MyCommand2" Command="local:Window8.MyCommand2" /> 
      </StackPanel> 
     </TabItem> 
     <TabItem Header="tabItem2" Name="tabItem2"> 
      <TabItem.CommandBindings> 
       <CommandBinding Command="local:Window8.MyCommand2" Executed="ExecuteMyCommand"/> 
      </TabItem.CommandBindings> 
      <StackPanel> 
       <Button Content="MyCommand1" Command="local:Window8.MyCommand1" /> 
       <Button Content="MyCommand2" Command="local:Window8.MyCommand2" /> 
      </StackPanel> 
     </TabItem> 
    </TabControl> 
</StackPanel> 
0

這是所有的代碼?

你有一個特殊的CanExecute定義,禁用MyCommandsX?
或者你已經綁定了綁定按鈕的啓用屬性,並且實現了INotifyPropertyChanged或者其他什麼?

或者爲什麼它們應該在您的代碼示例中啓用/禁用?
我你問我,我不希望代碼以禁用按鈕..

更新1:

您可以啓用按鈕,你所採取的方式相同,通過添加命令綁定例如在周圍的堆疊面板中。

<StackPanel> 
    <StackPanel.CommandBindings> 
      <CommandBinding Command="local:MainWindow.MyCommand1" 
          Executed="ExecuteMyCommand" /> 
    </StackPanel.CommandBindings> 
    <Button Content="MyCommand1" Command="local:MainWindow.MyCommand1" /> 
    <Button Content="MyCommand2" Command="local:MainWindow.MyCommand2" /> 
    <TabControl> 

您可以使用命令綁定的CanExecute部分來驗證啓用綁定按鈕的條件。 相反,你應該處理我認爲的命令本身。

+0

我現在睡覺了,明天我正在度假,所以我可能沒有時間很快回答你。對不起。祝你好運,快速找到一個好的解決方案 – SwissCoder 2010-07-26 22:12:13

+0

這是所有的代碼。我的問題是爲什麼當我更改標籤頁時,TabControl外部的按鈕沒有被禁用或啓用。我其實不是在問爲什麼。我只是問,我該怎麼做?特別是,如果選項卡是動態的,我不能直接編程給他們。 – 2010-07-26 22:21:35

0

您沒有任何可禁用按鈕的代碼。你可以用幾種方法做到這一點:

1.定義CanExecute事件處理程序。

<CommandBinding Command="local:MainWindow.MyCommand1" 
     Executed="ExecuteMyCommand" 
     CanExecute="MyCommand_CanExecute"/> 

後面的代碼:

private void MyCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
{ 
    e.CanExecute = tabItem1.IsSelected; 
} 

2.綁定按鈕IsEnabled屬性選項卡項目IsSelected財產

<Button IsEnabled="{Binding ElementName=tabItem1, Path=IsSelected}" 
     Content="MyCommand1" Command="local:MainWindow.MyCommand1" />