C# WPF选项卡项标题-强制按钮占用整个标题空间
我需要在单击WPF TabItem头时执行一个命令 由于Button有一个命令,我想我应该将该按钮放在TabItem标题中 我想让按钮占据TabItem标题的全部内容,但无论我做了什么尝试,我都无法让它这样做 这是我的简单XAML窗口: TestWindow.xamlC# WPF选项卡项标题-强制按钮占用整个标题空间,c#,wpf,xaml,C#,Wpf,Xaml,我需要在单击WPF TabItem头时执行一个命令 由于Button有一个命令,我想我应该将该按钮放在TabItem标题中 我想让按钮占据TabItem标题的全部内容,但无论我做了什么尝试,我都无法让它这样做 这是我的简单XAML窗口: TestWindow.xaml <Window x:Class="ActivePDFMonitor.TestWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/prese
<Window x:Class="ActivePDFMonitor.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestWindow" Height="300" Width="300">
<Grid>
<TabControl>
<TabItem Header="Tab 1">
<TextBlock Text="Hello, world"/>
</TabItem>
<TabItem>
<TabItem.HeaderTemplate>
<DataTemplate>
<Button Content="+" Background="Green"></Button>
</DataTemplate>
</TabItem.HeaderTemplate>
<TextBlock Text="Tab 2 contents"/>
</TabItem>
</TabControl>
</Grid>
</Window>
这是我尝试过的,但没有成功
HorizontalAlignment=“Stretch”
,VerticalAlignment=“Stretch”
,
HorizontalContentAlignment=“Stretch”
,VerticalContentAlignment=“Stretch”
-无视觉效果
-出于某种原因,这使得按钮占据了整个窗口<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="TabItem.Selected.Background" Color="#FFFFFF"/>
<SolidColorBrush x:Key="TabItem.Selected.Border" Color="#ACACAC"/>
<Style x:Key="TabControlStyle1" TargetType="{x:Type TabControl}">
<Setter Property="Padding" Value="2"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="{StaticResource TabItem.Selected.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource TabItem.Selected.Border}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabControl}">
<Grid x:Name="templateRoot" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
<Grid.RowDefinitions>
<RowDefinition x:Name="RowDefinition0" Height="Auto"/>
<RowDefinition x:Name="RowDefinition1" Height="*"/>
</Grid.RowDefinitions>
<!-- here is the edit -->
<DockPanel x:Name="headerPanel" Background="Transparent" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1" />
<Border x:Name="contentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
<ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TabControl Style="{DynamicResource TabControlStyle1}">
<TabItem Header="Tab 1" DockPanel.Dock="Left">
<TextBlock Text="Hello, world"/>
</TabItem>
<TabItem Header="Tab 2" DockPanel.Dock="Left">
<TextBlock Text="Hello, world"/>
</TabItem>
<TabItem Header="Foo bar" />
</TabControl>
</Grid>
</Window>
WPF的控制装置不会那样摆动。您需要重新设置模板以使用其他面板。产生您请求的效果:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="TabItem.Selected.Background" Color="#FFFFFF"/>
<SolidColorBrush x:Key="TabItem.Selected.Border" Color="#ACACAC"/>
<Style x:Key="TabControlStyle1" TargetType="{x:Type TabControl}">
<Setter Property="Padding" Value="2"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="{StaticResource TabItem.Selected.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource TabItem.Selected.Border}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabControl}">
<Grid x:Name="templateRoot" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
<Grid.RowDefinitions>
<RowDefinition x:Name="RowDefinition0" Height="Auto"/>
<RowDefinition x:Name="RowDefinition1" Height="*"/>
</Grid.RowDefinitions>
<!-- here is the edit -->
<DockPanel x:Name="headerPanel" Background="Transparent" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1" />
<Border x:Name="contentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
<ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TabControl Style="{DynamicResource TabControlStyle1}">
<TabItem Header="Tab 1" DockPanel.Dock="Left">
<TextBlock Text="Hello, world"/>
</TabItem>
<TabItem Header="Tab 2" DockPanel.Dock="Left">
<TextBlock Text="Hello, world"/>
</TabItem>
<TabItem Header="Foo bar" />
</TabControl>
</Grid>
</Window>
谢谢你的富有洞察力的回复。你确实用我问的方式回答了这个问题(并教会了我一些东西),所以我投了赞成票并接受了。。。我想我只是说得不够好。我实际上是在寻找TabItem“+”按钮是否具有零边距/零填充。我发现TabItem标题有一个默认边距,所以我正在进行。再次感谢,太棒了,谢谢你。太酷了,我不知道你可以完全覆盖TabItem的模板。谢谢你的富有洞察力的回复。你确实用我问的方式回答了这个问题(并教会了我一些东西),所以我投了赞成票并接受了。。。我想我只是说得不够好。我实际上是在寻找TabItem“+”按钮是否具有零边距/零填充。我发现TabItem标题有一个默认边距,所以我正在进行。再次感谢,太棒了,谢谢你。这很酷,我不知道你可以完全覆盖TabItem的模板。