带有每个选项卡绑定的 WPF 路由命令

发布于 2024-09-11 16:07:59 字数 2210 浏览 6 评论 0原文

我打算禁用和启用 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");
    }

I intended to disable and enable the Buttons outside the TabControl, just like those inside the TabItem when the current tab is changed. But the CommandBindings of the TabItem do not seem to impact "up" the visual tree. What is the right way to do this?

With this 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>

With this Code Behind:

    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");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佼人 2024-09-18 16:07:59

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>

MSFT gave me the correct answer in their WPF forum, here (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>
却一份温柔 2024-09-18 16:07:59

您没有任何可以禁用按钮的代码。您可以通过多种方式做到这一点:

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" />

Your don't have any code that could disable buttons. You can do it in several ways:

1. Define CanExecute event handler.

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

Code behind:

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

2. Bind button IsEnabled property to tab item IsSelected property

<Button IsEnabled="{Binding ElementName=tabItem1, Path=IsSelected}" 
        Content="MyCommand1" Command="local:MainWindow.MyCommand1" />
來不及說愛妳 2024-09-18 16:07:59

这是全部代码吗?

您定义了一个特殊的 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 部分来验证绑定按钮启用的条件。
相反,我认为你应该处理命令本身。

Is this all the code?

You have a special CanExecute defined, that disables the MyCommandsX ?
Or you have a binding on the Enabled Property of the bound buttons, and you implement INotifyPropertyChanged or something?

Or why should they be enabled/disabled in your code sample?
I you ask me, I wouldn't expect the code to disable the buttons..

Update 1:

You could enable the buttons the same way you did it, by adding the command bindings in the surrounding stackpanel for example.

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

You can use the CanExecute part of the command binding to verify the conditions under which the bound buttons enabled.
Instead you should handle the command itself I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文