I have some workspaces presented inside a TabControl
. Each workpace has some command bindings bound to some ApplicationCommands
like
The menu is build by using these ApplicationCommands
<Menu>
<MenuItem Command="ApplicationCommands.New"/>
<MenuItem Command="ApplicationCommands.Save"/>
<MenuItem Command="ApplicationCommands.Close"/>
</Menu>
It is very easy to use this commands when the TabControl
is wired manually
<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>
When I select just the TabItem
I can use the menu to execute the commands.
But the workspaces are not static, so I had to bind to a colletion of workspaces. Now it is not enough to select the TabItem
, I also had to activate the content, to use the commands from the menu (not suprising, because the TabItem
is active without any command binding)
<TabControl ItemsSource="{Binding Path=Workspaces}">
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="HeaderTemplate" Value="{StaticResource ClosableTabItemTemplate}"/>
</Style>
</TabControl.Resources>
</TabControl>
Here the DataTemplate for the 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>
How can I set the CommandBindings to the dynamic created TabItem
or how do I get the TabItem
itself to use my AttachedProperties.RegisterCommandBindings
?
Update
As a workaround (maybe it is the only possible solution) I bind the commands to the TabControl
itself
<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>
Did you try to set the attached property of the item container?:
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.