需要WPF控制建议

时间:2011-05-31 21:41:26

标签: wpf

我有一个标签控件,我想在右上角添加一个按钮。

> [TabControl]
> [TabItem1][TabItem2][TabItem3][TabItem4].....blank space...[ settings button] 

如果我将设置按钮添加到TabControl,它将直接与TabItem4相邻。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

为您放入的TabControl创建类似模板内容的内容。

e.g。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="100"/>
    </Grid.ColumnDefinitions>       
    <TabControl Grid.ColumnSpan="2" >
            <TabItem Header="TabItem">
            </TabItem>
            <TabItem Header="TabItem">
            </TabItem>
    </TabControl>
    <Button Grid.Column="1" VerticalAlignment="Top" Height="20"></Button>
</Grid>

或者,您可以根据需要编辑TabControl模板结束自定义HeaderPanel。

<ControlTemplate TargetType="{x:Type TabControl}">
    <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="ColumnDefinition0"/>
            <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
            <RowDefinition x:Name="RowDefinition1" Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Column="0">
            <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="100"/>
        </Grid.ColumnDefinitions>
        <TabPanel x:Name="HeaderPanel" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
        <Button Grid.Column="1"> settings </Button>
    </Grid>
    <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>