0

我尝试在 Visual Studio 2012 中创建类似于 WPF 中的功能选项卡控件的外观。但我无法创建它。谁能告诉我该怎么做?

4

1 回答 1

0

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);
        }

    }
}

我希望这会有所帮助。如果您需要更多详细信息,请告诉我。

于 2012-10-05T18:02:16.463 回答