2017-02-09 6 views
1

TabControlの中にいくつかのワークスペースがあります。各workpaceはItemsSourceを使用するときにTabItem CommandBindingsを設定する方法

  • 新(フー)のようないくつかのApplicationCommandsにバインドされたいくつかのコマンドバインディング
  • 保存(バー)
  • 閉じる(フー、バー)

メニューは、これらを使用して構築されていApplicationCommands

<Menu> 
    <MenuItem Command="ApplicationCommands.New"/> 
    <MenuItem Command="ApplicationCommands.Save"/> 
    <MenuItem Command="ApplicationCommands.Close"/> 
</Menu> 

それverはあります私はちょうどTabItemは私がコマンドを実行するためにメニューを使用することができます選択するとyが簡単TabControlを手動

<TabControl> 
    <TabControl.Resources> 
     <Style TargetType="TabItem"> 
      <Setter Property="HeaderTemplate" Value="{StaticResource ClosableTabItemTemplate}"/> 
     </Style> 
    </TabControl.Resources> 
    <TabItem DataContext="{Binding Foo}" 
      Header="{Binding}" 
      Content="{Binding}" 
      local:AttachedProperties.RegisterCommandBindings="{Binding Path=CommandBindings}"/> 
    <TabItem DataContext="{Binding Bar}" 
      Header="{Binding}" 
      Content="{Binding}" 
      local:AttachedProperties.RegisterCommandBindings="{Binding Path=CommandBindings}"/> 
</TabControl> 

に配線されているときに、このコマンドを使用します。

しかし、ワークスペースは静的ではないため、ワークスペースの列にバインドする必要がありました。今ではTabItemを選択するだけでは十分ではない。ここ

<TabControl ItemsSource="{Binding Path=Workspaces}"> 
    <TabControl.Resources> 
     <Style TargetType="TabItem"> 
      <Setter Property="HeaderTemplate" Value="{StaticResource ClosableTabItemTemplate}"/> 
     </Style> 
    </TabControl.Resources> 
</TabControl> 

TabItemが結合任意のコマンドなしでアクティブになっているので、思わぬではない)、私はまた、メニューからコマンドを使用して、コンテンツをアクティブにする必要がありました私はTabItemを作成したり、どのように私はTabItem自体が私のを使用するのですかダイナミックにCommandBindingsを設定するにはどうすればよいTabItem

<DataTemplate x:Key="ClosableTabItemTemplate"> 
    <DockPanel LastChildFill="True"> 
     <Button Content="X" DockPanel.Dock="Right" Command="{Binding Path=CloseCommand}"/> 
     <TextBlock Text="{Binding Path=DisplayName}"/> 
    </DockPanel> 
</DataTemplate> 

用のDataTemplate?回避策として

更新

(多分それは唯一の可能な解決策である)私はTabControl自体

<TabControl ItemsSource="{Binding Path=Workspaces}" 
      local:AttachedProperties.RegisterCommandBindings="{Binding RelativeSource={RelativeSource Self},Path=SelectedItem.CommandBindings}"> 
    <TabControl.Resources> 
     <Style TargetType="TabItem"> 
      <Setter Property="HeaderTemplate" Value="{StaticResource ClosableTabItemTemplate}"/> 
     </Style> 
    </TabControl.Resources> 
</TabControl> 

答えて

2

にコマンドをバインドするあなたがの添付プロパティを設定しようとしましたアイテムコンテナ:

<TabControl ItemsSource="{Binding Path=Workspaces}"> 
    <TabControl.ItemContainerStyle> 
     <Style TargetType="TabItem"> 
      <Setter Property="local:AttachedProperties.RegisterCommandBindings" Value="{Binding RelativeSource={RelativeSource Self}, Path=CommandBindings}" /> 
      <Setter Property="HeaderTemplate" Value="{StaticResource ClosableTabItemTemplate}"/> 
     </Style> 
    </TabControl.ItemContainerStyle> 
</TabControl> 
+0

これは私が探していたものでしたいいぞ –

関連する問題