簡體   English   中英

我如何在Visual Studio 2012中創建Tab Contorl

[英]How Can i create Tab Contorl in Visual Studio 2012

我試圖在Visual Studio 2012的WPF中創建類似於功能選項卡控件的外觀。但是我無法創建它。 誰能告訴我該怎么做?

WPF中有一個TabControl。 沒錯,它看起來不像VS 2010中的那些,但它們在那里。 剩下的只是樣式問題。 (除非我缺少任何東西)。 只需按“ P”鍵即可更改主題。

<Window x:Class="WpfApplication6.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" 
    Loaded="Window_Loaded"
    KeyUp="Window_KeyUp"
    >
<Grid>
    <TabControl>
        <TabItem Header="Test 1" >
            <Grid>
                <Button Content="Test 1" />
            </Grid>
        </TabItem>
        <TabItem Header="Test 2" >
            <Grid>
                <Button Content="Test 2" />
            </Grid>
        </TabItem>
        <TabItem Header="Test 3" >
            <Grid>
                <Button Content="Test 3" />
            </Grid>
        </TabItem>
        <TabItem Header="Test 4" >
            <Grid>
                <Button Content="Test 4" />
            </Grid>
        </TabItem>
    </TabControl>
</Grid>
</Window>

這將生成一個簡單的選項卡控件。 至於樣式,您可以應用許多樣式。 這是我為此創建的幫助器類:

static class ThemeManager
{
    private static ResourceDictionary[] _themes;
    public static ResourceDictionary AeroTheme;
    //public static ResourceDictionary ClassicTheme;
    public static ResourceDictionary LunaTheme;
    public static ResourceDictionary RoyaleTheme;

    private static int _currentIndex = 0;

    public static ResourceDictionary GetNextTheme()
    {
        ResourceDictionary vDict = _themes[_currentIndex];

        if (_currentIndex == _themes.GetUpperBound(0))
            _currentIndex = 0;
        else
            _currentIndex++;

        return vDict;
    }

    static ThemeManager()
    {
        List<ResourceDictionary> vList = new List<ResourceDictionary>();
        Uri themeUri;
        //************
        themeUri = new Uri("PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component\\themes/aero.normalcolor.xaml", UriKind.Relative);
        AeroTheme = (ResourceDictionary)Application.LoadComponent(themeUri);
        vList.Add(AeroTheme);
        //************
        themeUri = new Uri("PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component\\themes/Luna.normalcolor.xaml", UriKind.Relative);
        LunaTheme = (ResourceDictionary)Application.LoadComponent(themeUri);
        vList.Add(LunaTheme);
        //************
        themeUri = new Uri("PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component\\themes/Luna.homestead.xaml", UriKind.Relative);
        LunaTheme = (ResourceDictionary)Application.LoadComponent(themeUri);
        vList.Add(LunaTheme);
        //************
        themeUri = new Uri("PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component\\themes/Luna.metallic.xaml", UriKind.Relative);
        LunaTheme = (ResourceDictionary)Application.LoadComponent(themeUri);
        vList.Add(LunaTheme);
        //************
        themeUri = new Uri("PresentationFramework.Royale;V3.0.0.0;31bf3856ad364e35;component\\themes/Royale.normalcolor.xaml", UriKind.Relative);
        RoyaleTheme = (ResourceDictionary)Application.LoadComponent(themeUri);
        vList.Add(RoyaleTheme);
        //************
        /*ClassicTheme = null;
        vList.Add(ClassicTheme);*/
        //************

        _themes = vList.ToArray();
    }
}

這是Window類(允許切換主題):

public partial class MainWindow : Window
{
    ResourceDictionary _dict;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        _dict = ThemeManager.GetNextTheme();
        Resources.MergedDictionaries.Add(_dict);
    }

    private void Window_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.P) {
            if (_dict != null)
                Resources.MergedDictionaries.Remove(_dict);

            _dict = ThemeManager.GetNextTheme();
            Resources.MergedDictionaries.Add(_dict);
        }

    }
}

我希望這會有所幫助。 讓我知道您是否需要更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM