0

我是 wpf 的新手,我想在 ButtonClick 上更改我的 TapStripPlacement。我尝试自己找到解决方案,但无法做到。我会对快速解决方案感到非常高兴。

这是我的代码:

    <Window x:Class="Pages.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TabControl" Height="350" Width="525">


    <Window.Resources>

    <SolidColorBrush x:Key="Foreground" Color="#FF9531b1"></SolidColorBrush>
    <SolidColorBrush x:Key="Background" Color="Transparent"></SolidColorBrush>

    <ControlTemplate x:Key="TabItemControlTemplate" TargetType="{x:Type TabItem}">
            <Grid SnapsToDevicePixels="True">
                <Border x:Name="Bd" BorderBrush="{StaticResource Foreground}" BorderThickness="0" Padding="{TemplateBinding Padding}">
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                        <Setter Property="Background" Value="{StaticResource Background}"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" Value="True">
                                    <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsMouseOver}" Value="True">
                                    <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <ContentPresenter x:Name="Content" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
                </Border>
            </Grid>
            <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="Bd" Property="Background" Value="{StaticResource Foreground}"></Setter>
                <Setter TargetName="Content" Property="TextBlock.Foreground" Value="White"></Setter>
                <Setter TargetName="Content" Property="TextBlock.FontWeight" Value="Heavy"></Setter>
            </Trigger>

            <MultiTrigger >
                <MultiTrigger.Conditions >
                    <Condition Property="IsSelected" Value="True"></Condition>
                    <Condition Property="IsMouseOver" Value="false"></Condition>
                </MultiTrigger.Conditions>
                <Setter TargetName="Content" Property="TextBlock.Foreground" Value="{StaticResource Foreground}"></Setter>
                <Setter TargetName="Content" Property="TextBlock.FontWeight" Value="Heavy"></Setter>
            </MultiTrigger>

        </ControlTemplate.Triggers>

        </ControlTemplate>

</Window.Resources>


    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <StackPanel Grid.Column="1">
        <Button x:Name="btnTop">Top</Button>
        <Button Name="btnRight">Right</Button>
        <Button Name="btnBottom">Bottom</Button>
        <Button Name="btnLeft">Left</Button>
    </StackPanel>

    <TabControl Grid.Column="0"  x:Name="MyControl" Height="200"
                    Background="Transparent"
                    BorderThickness="0,1,0,0"
                    BorderBrush="{StaticResource Foreground}" TabStripPlacement="Top">
            <TabItem Header="tab1" Background="Transparent"   Template="{DynamicResource TabItemControlTemplate}"/>
            <TabItem Header="tab2" Background="Transparent" Template="{DynamicResource TabItemControlTemplate}"/>
            <TabItem Header="tab3" Background="Transparent" Template="{DynamicResource TabItemControlTemplate}"/>

    </TabControl>

</Grid>

提前致谢

4

1 回答 1

0

试试看

<DockPanel>
    <StackPanel Grid.Column="1" DockPanel.Dock="Top" Orientation="Horizontal">
        <Button x:Name="btnTop" Click="btnTop_Click">Top</Button>
        <Button Name="btnRight" Click="btnRight_Click">Right</Button>
        <Button Name="btnBottom" Click="btnBottom_Click">Bottom</Button>
        <Button Name="btnLeft" Click="btnLeft_Click">Left</Button>
    </StackPanel>
    <TabControl Name="tabControl" DockPanel.Dock="Bottom">
    <TabItem Header="Tab1"></TabItem>
    <TabItem Header="Tab2"></TabItem>
</TabControl>
</DockPanel>

在您的文件后面的代码中

    private void btnTop_Click(object sender, RoutedEventArgs e)
    {
        tabControl.TabStripPlacement = Dock.Top;
    }

    private void btnRight_Click(object sender, RoutedEventArgs e)
    {
        tabControl.TabStripPlacement = Dock.Right;
    }

    private void btnBottom_Click(object sender, RoutedEventArgs e)
    {
        tabControl.TabStripPlacement = Dock.Bottom;
    }

    private void btnLeft_Click(object sender, RoutedEventArgs e)
    {
        tabControl.TabStripPlacement = Dock.Left;
    } 
于 2013-07-18T09:31:27.290 回答