我打算禁用和啓用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");
}
我現在睡覺了,明天我正在度假,所以我可能沒有時間很快回答你。對不起。祝你好運,快速找到一個好的解決方案 – SwissCoder 2010-07-26 22:12:13
這是所有的代碼。我的問題是爲什麼當我更改標籤頁時,TabControl外部的按鈕沒有被禁用或啓用。我其實不是在問爲什麼。我只是問,我該怎麼做?特別是,如果選項卡是動態的,我不能直接編程給他們。 – 2010-07-26 22:21:35