2013-10-31 29 views
0

我有一个mainwindow,其中有一个包含listview的usercontrol。 用户控件也有一个按钮,它将listview的所有内容复制到剪贴板。 这是如何实现复制功能的。下面 是用户控件的XAML的一部分 -将mainwindow视图模型中的KeyBinding命令链接到用户控件的视图模型commandbindings

<Button Command="Copy" 
    CommandTarget="{Binding ElementName=testCodeView}" 
    CommandParameter="Copy" 
</Button> 

<ListView x:Name="testCodeView" 
     ItemsSource="{Binding Products}" BorderThickness="0" Grid.Row="1" 
     ItemTemplate="{StaticResource testViewTemplate}"  
     ItemContainerStyle="{StaticResource testCodesListItem}" 
     infra:AttachedProperties.CommandBindings ="{Binding CommandBindings}"> 
</ListView> 

的AttachedProperties类持有的依赖属性“化CommandBindings” - 下面的代码 -

public class AttachedProperties 
{ 
public static DependencyProperty CommandBindingsProperty = 
     DependencyProperty.RegisterAttached("CommandBindings", typeof(CommandBindingCollection), typeof(AttachedProperties), 
     new PropertyMetadata(null, OnCommandBindingsChanged)); 
public static void SetCommandBindings(UIElement element, CommandBindingCollection value) 
    { 
     if (element != null) 
      element.SetValue(CommandBindingsProperty, value); 
    } 
    public static CommandBindingCollection GetCommandBindings(UIElement element) 
    { 
     return (element != null ? (CommandBindingCollection)element.GetValue   (CommandBindingsProperty) : null); 
    } 
} 

下面是有关复制的用户控件视图模型的代码项目的listview。

public class UserControlViewModel : INotifyPropertyChanged 
{ 
    public CommandBindingCollection CommandBindings 
    { 
     get 
     { 
      if (commandBindings_ == null) 
      { 
       commandBindings_ = new CommandBindingCollection(); 
      } 
      return commandBindings_; 
     } 
    } 
    public UserControlViewModel 
    { 
     CommandBinding copyBinding = new CommandBinding(ApplicationCommands.Copy, 
     this.CtrlCCopyCmdExecuted, this.CtrlCCopyCmdCanExecute); 
     // Register binding to class 
     CommandManager.RegisterClassCommandBinding(typeof(UserControlViewModel), copyBinding); 
     this.CommandBindings.Add(copyBinding); 
    } 
    private void CtrlCCopyCmdExecuted(object sender, ExecutedRoutedEventArgs e) 
    { 
     copyToclipboard_.CtrlCCopyCmdExecuted(sender, e); 
    } 
} 

在CtrlCCopyCmdExecuted功能发送者对象是在用户控件的列表视图,其在futher使用 复制其内容。 复制所有功能在用户控制按钮上正常工作。 我必须在mainwindow中为复制功能创建一个键绑定。我在mainwindow中创建了其他键绑定,因为这些命令在MainWindowViewModel中定义,但由于copy all命令的命令绑定位于usercontrol的视图模型中,所以我在将mainwindowviewmodel中的键绑定命令与usercontrolviewmodel中的命令绑定。 有人可以帮我解决这个问题。

在此先感谢。

回答

0

如果您的父视图模型有权访问子视图模型,那么您有两个主要选项。这两种方法都涉及创建一个ICommand属性Bind在父视图模型:

<KeyBinding Gesture="CTRL+C" Command="{Binding Copy, Mode=OneWay}" /> 

第一个选项包括创建一个BaseViewModel类中有一个abstract ICommand财产......它扩展了BaseViewModel有任何类实现这个命令,那么你可以有这种类型的子视图模型属性和简单地传递沿着价值:要对命令

public override ICommand Copy 
{ 
    get { return new ActionCommand(action => ViewModel.Copy.Execute(null), 
canExecute => ViewModel.Copy != null); } 
} 

我怀疑尽管每个视图模型都是,所以我们来调查选项二。其基本思想是,如果你有机会到子视图模型,你可随着孩子“通行证”的命令:

public ICommand Copy 
{ 
    get { return new ActionCommand(action => childViewModel.Copy.Execute(action), 
canExecute => childViewModel.Copy.Execute(canExecute)); } 
} 

ActionCommand是基于通用RelayCommand自定义类。


UPDATE >>>

好了,我以前从未使用过CommandBindings,因为它使用我ActionCommand,或RelayCommand所以容易得多,但我刚刚调查了它在MSDN和两个想法会想起来。

首先,如果您在子项UserControl中使用ApplicationCommands.Copy命令对象,那么当然可以在父视图中使用KeyBinding中的同一对象。ApplicationCommandsstaticCopystatic属性,因此无论你怎么称呼它,它是相同对象:

<KeyBinding Gesture="CTRL+C" Command="ApplicationCommands.Copy" /> 

如果做不到这一点,你可以在你的父视图模型创建ICommand财产,要么创建自己的Copy命令,或者只是返回ApplicationCommands.Copy命令。然后,您可以从父视图子视图中将Bind添加到该视图中...或者,您甚至可以将它从父视图模型添加到子视图CommandBinding对象中。

+0

谢谢谢里登。但是我可能在实施这个解决方案时遇到一些问题。 childViewModel没有特定的命令来复制。它使用称为Commandbindings的依赖项属性(请参阅上面的usercontrol xaml和AttachedProperties类)。我有权访问parenViewModel中的childViewModel的Commandbindings。无论如何,我可以用它来执行命令。另外Executed eventhandler中的sender对象应该是listview(复制按钮的commandtarget是listview)。 – rajat