2015-06-17 98 views
0

我目前正努力工作,这在我的脑海似乎并不那么辛苦。命令绑定到UserControl

A获得其显示在窗口中的顶层用户控制:

<UserControl.Resources> 

    <DataTemplate DataType="{x:Type viewModels:PCodeViewModel}"> 
     <controls:PCodeTabControl /> 
    </DataTemplate> 
    <DataTemplate x:Key="TabItemHeaderTemplate"> 
     <TextBlock FontWeight="Medium" Text="{Binding}" /> 
    </DataTemplate> 
    <Style x:Key="TabItemStyle" TargetType="{x:Type dx:DXTabItem}"> 
     <Setter Property="Header" Value="{Binding TabHeader}" /> 
     <Setter Property="Content" Value="{Binding}" /> 
    </Style> 
    <DataTemplate DataType="{x:Type viewModels:MexCompileViewModel}"> 
     <controls:MexCompileTabControl /> 
    </DataTemplate> 
</UserControl.Resources> 
<Grid> 
    <dx:DXTabControl ItemContainerStyle="{StaticResource TabItemStyle}" 
        ItemHeaderTemplate="{StaticResource TabItemHeaderTemplate}" 
        ItemsSource="{Binding Tabs}" /> 
</Grid> 

对应的视图模型是在这里:

private ICommand createNewProjectCommand; 

    private string sandboxRoot; 

    public MatlabBuildViewModel() 
    { 
     this.Init(); 
    } 

    public void Init() 
    { 
     this.InitTabs(); 
    } 

    public void InitTabs() 
    { 
     this.Tabs = new ObservableCollection<TabViewModelBase> 
         { 
          new MexCompileViewModel(), 
          new PCodeViewModel() 
         }; 
     this.SandboxRoot = @"E:\_SupportTools\CaseManager"; 
    } 

    public ObservableCollection<TabViewModelBase> Tabs { get; private set; } 

    public void NotifyChildren() 
    { 
     Messenger.Default.Send(new SandboxRootUpdated()); 
    } 

    public string SandboxRoot 
    { 
     get 
     { 
      return this.sandboxRoot; 
     } 

     set 
     { 
      if (value != null) 
      { 
       this.sandboxRoot = value; 
       this.OnPropertyChanged(); 
       this.NotifyChildren(); 
      } 
     } 
    } 

    /// <summary> 
    /// Gets the create new project command. 
    /// </summary> 
    public ICommand CreateEmptyProjectCommand 
    { 
     get 
     { 
      if (this.createNewProjectCommand == null) 
      { 
       this.createNewProjectCommand = new DelegateCommand(Debugger.Break); 
      } 

      return this.createNewProjectCommand; 
     } 
    } 

现在你可以看到我由具有显示两个标签用于targetType MexCompileViewModel和PCodeViewModel的DataTemplate。

Dattemplate绑定的两个userControl共享一个包含多个按钮的普通UserControl。

这里是MexCompileTabControl为例

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="Auto" /> 
    </Grid.ColumnDefinitions> 
    <compositeControls:MexCompileGrid Grid.Column="0" IsEnabled="{Binding IsEnabled}" /> 
    <StackPanel Grid.Column="1"> 
     <compositeControls:CommonActionsControl /> 
    </StackPanel> 
</Grid> 

的CommonActionsControl只是带有按钮的StackPanel中:

<StackPanel helpers:MarginSetter.Margin="3"> 
    <GroupBox Header="MatlabProject-File"> 
     <StackPanel helpers:MarginSetter.Margin="3"> 
      <Button Command="{Binding CreateEmptyProjectCommand}" Content="Create empty project-file" /> 
      <Button Content="Refresh" /> 
     </StackPanel> 
    </GroupBox> 
    <GroupBox Header="Actions"> 
     <StackPanel helpers:MarginSetter.Margin="3"> 
      <Button Content="Clean" /> 
      <Button Content="Rebuild" /> 
      <Button Content="Generate m-Script" /> 
     </StackPanel> 
    </GroupBox> 
</StackPanel> 

代码背后:

 public CommonActionsControl() 
    { 
     this.InitializeComponent(); 
    } 

    public static readonly DependencyProperty CreateEmptyProjectCommandProperty = DependencyProperty.Register("CreateEmptyProjectCommand", typeof(ICommand), typeof(CommonActionsControl), new PropertyMetadata(default(ICommand))); 

    public ICommand CreateEmptyProjectCommand 
    { 
     get 
     { 
      return (ICommand)GetValue(CreateEmptyProjectCommandProperty); 
     } 

     set 
     { 
      this.SetValue(CreateEmptyProjectCommandProperty, value); 
     } 
    } 

所以我想实现是: 我的命令在TopLevelViewModel。现在我想我的CommonActionsControl继承这些命令,因为控制应该多次使用。你能帮助我吗?

回答

0

由于CommonActionsControl显示在TopLevelViewModel中定义的常见操作,因此将它们作为TopLevelView的一部分而不是在每个选项卡上显示它们都是有意义的。

如果你确实希望每个选项卡上都有他们,那么我认为你最好的选择是将命令添加到你的TabViewModelBase,以便各种选项卡视图可以绑定到它们。你仍然可以在TopLevelViewModel中自由实现它们,只需将它们通过构造函数注入到各个选项卡虚拟机中即可。