1

我打算禁用和启用 TabControl 外部的按钮,就像更改当前选项卡时 TabItem 内部的按钮一样。但是 TabItem 的 CommandBindings 似乎不会影响“向上”视觉树。这样做的正确方法是什么?

使用此 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");
    }
4

3 回答 3

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>
于 2010-08-05T08:56:14.797 回答
0

这是所有的代码吗?

你有一个特殊的 CanExecute 定义,禁用 MyCommandsX ?
或者您对绑定按钮的 Enabled 属性进行了绑定,并且您实现了 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 部分来验证绑定按钮启用的条件。相反,我认为您应该处理命令本身。

于 2010-07-26T22:01:33.027 回答
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" />
于 2010-08-01T17:34:29.587 回答