2017-11-11 245 views
0

我一直在開發一些簡單的應用程序(只是教育目的)使用Windows窗體,現在我開始學習WPF,但似乎更復雜一點給我。如何創建與左側用WPF標籤的一個簡單的菜單?

我想創建這樣的一個你可以在圖片中看到一個簡單的菜單。我一直試圖找到它在谷歌和YouTube,但我只是找到了很多教程關於「漢堡包菜單」,這是不是我要找的。

Ultimate Windows Tweaker

Ultimate Windows Tweaker

Ultimate Windows Tweaker

回答

1

這是真的,其中WPF眼前一亮的地區:你描述的佈局可以用什麼比一個重新設計的標籤控制多個。

開始與TabStripPlacementLeft的無樣式選項卡控件:

Unstyled tabstrip left

然後編輯TabControl風格添加漸變和利潤率TabPanel背後:

tabcontrol with unstyled tabitems

添加圖像並覆蓋TabItem樣式以設置字體並刪除黑色回合。我用了一個V形並指示選擇,而不是加粗的文字,但會被同樣的方式處理。

completed TabControl

下面是用於截圖上面全XAML:

<Window x:Class="StackOverflowTabControl.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:StackOverflowTabControl" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/> 
     <Style x:Key="ItemContainerStyle" TargetType="{x:Type TabItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type TabItem}"> 
         <Grid Margin="15,5"> 
          <Path Width="10" Height="14" Margin="0,2,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" Stretch="Fill" Fill="#FF000000" Data="F1 M 39.8307,37.6042L 36.6641,34.4375L 25.1849,23.3542L 35.4766,23.3542L 50.5182,37.6042L 35.4766,51.8542L 25.1849,51.8542L 36.6641,40.7708L 39.8307,37.6042 Z " Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource TemplatedParent}}"/> 
          <ContentPresenter Content="{TemplateBinding Header}" Margin="20,5,10,5"> 
           <ContentPresenter.Resources> 
            <Style TargetType="{x:Type TextBlock}"> 
             <Setter Property="FontSize" Value="18" /> 
             <Setter Property="FontWeight" Value="Light" /> 
            </Style> 
           </ContentPresenter.Resources> 
          </ContentPresenter> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
     <Style x:Key="TabControlStyle1" TargetType="{x:Type TabControl}"> 
      <Setter Property="TabStripPlacement" Value="Left" /> 
      <Setter Property="ItemContainerStyle" Value="{StaticResource ItemContainerStyle}" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type TabControl}"> 
         <Grid x:Name="templateRoot" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="Auto" /> 
           <ColumnDefinition Width="1*" /> 
          </Grid.ColumnDefinitions> 
          <Border Grid.Column="0" Padding="5"> 
           <Border.Background> 
            <LinearGradientBrush EndPoint="0,0" StartPoint="1,0"> 
             <GradientStop Color="#FFC7C7C7" Offset="0"/> 
             <GradientStop Color="#FFECECEC" Offset="1"/> 
            </LinearGradientBrush> 
           </Border.Background> 
           <DockPanel> 
            <Image DockPanel.Dock="Bottom" HorizontalAlignment="Center" Margin="20" Source="pack://application:,,,/StackOverflowTabControl;component/twc.png" Width="200" /> 
            <TabPanel x:Name="headerPanel" Background="Transparent" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/> 
           </DockPanel> 
          </Border> 
          <Border x:Name="contentPanel" Grid.Column="1" Margin="5,0,0,0"> 
           <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
          </Border> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <TabControl Style="{DynamicResource TabControlStyle1}"> 
      <TabItem Header="System Information"> 
       <TextBlock Text="Windows 10 pro" /> 
      </TabItem> 
      <TabItem Header="Something else"> 
       <TextBlock Text="Other page" /> 
      </TabItem> 
     </TabControl> 
    </Grid> 
</Window> 
+0

非常感謝你,米奇!這是一個非常好的和詳細的解釋。對此,我真的非常感激。我已經接受了你的答案! – AlexMnrs