级联命令绑定可能

时间:2015-03-19 12:33:37

标签: c# wpf mvvm

我有一个MainWindow,它包含使用MVVM的多个视图。事实上,MainWindow只包含一个模型列表,每次添加一个模型时,它都会创建一个View(这里是一个UserControl)并将DataContext(Model)与View相关联。每个视图都放在TabControl中的单独TabItem中。

Model本身有一个CommandBindingsCollection,并将此命令绑定分配给View。这意味着例如F10可以多次使用,但是只有活动的View应该在F10上做出反应。

但是,F10根本不起作用。只有当我将CommandBinding分配给MainWindow它才有效,但这会使View依赖于UserControl而这不是我想要的,因为我想从MainWindow创建尽可能独立的View。

我唯一的解决方案是使其动态拦截当前TabItem的更改并添加/删除活动View的命令。目前一切都没有代码,但我必须为它编写代码。

附件是MainWindow的代码:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
</Window>



 using System.Windows;                 

 namespace WpfApplication3
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();

                PluginControl aControl = new PluginControl();
                Content = aControl;
            }
        }
    }

和UserControl的代码:

<UserControl x:Class="WpfApplication3.PluginControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication3"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Background="White">
    <StackPanel>
        <Button Command="{Binding myCommand}" Content="PushMe" Focusable="False"/>
        <Label Content="Nothing pressed" Name="myLabel"/>            
    </StackPanel>
</UserControl>

using System;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for PluginControl.xaml
    /// </summary>
    public partial class PluginControl : UserControl
    {
        public RoutedCommand myCommand
        {
            get;
            private set;
        }
        public PluginControl()
        {
            InitializeComponent();

            DataContext = this;

            myCommand = new RoutedCommand();
            myCommand.InputGestures.Add(new KeyGesture(Key.F10));

            CommandBindings.Add(new CommandBinding(myCommand, myCommandHandler));
        }

        private void myCommandHandler(object sender, ExecutedRoutedEventArgs executed)
        {
            myLabel.Content = DateTime.Now.ToString();
        }
    }
}

如你所见,我已将Button设置为Focusable为false,因为我希望命令一般工作,而不仅仅是按钮聚焦时。我错过了一些东西或者想错了方向,那么添加一个CommandBinding并不意味着命令在没有绑定的情况下工作了吗?

将命令添加到MainWindow本身可以正常工作。

任何帮助?

0 个答案:

没有答案
相关问题