Visual Studio 2012 の WPF で機能タブ コントロールのようなものを作成しようとしましたが、作成できませんでした。どうすればそれができるか教えてもらえますか?
1781 次
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 に答える