テンプレート(Templates)
概要
すべてのWPFコントロールは"見た目"を持たないように設計されている.ただし,既定の見た目を持っており,これからVisualTreeが作成される.この見た目を決めるものがコントロールテンプレートと呼ばれるものであり,XAMLで定義されている.
コントロールの標準のテンプレートを表示する
+
|
コントロールの標準のテンプレートを表示するサンプルプログラム |
XAML
<Window x:Class="WpfSampleApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="1000"
Loaded="Window_Loaded">
<StackPanel Orientation="Horizontal" Name="layoutRoot">
<TreeView Name="lstTypes" SelectedItemChanged="lstTypes_SelectedItemChanged" Width="400"/>
<TextBox Name="txtTemplate" Width="600" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"/>
</StackPanel>
</Window>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
using System.Reflection;
using System.Xml;
using System.Windows.Markup;
namespace WpfSampleApplication
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Type controlType = typeof(Control);
List<Type> derivedTypes = new List<Type>();
// Search all the types in the assembly where the Control class is defined.
Assembly assembly = Assembly.GetAssembly(typeof(Control));
foreach (Type type in assembly.GetTypes())
{
// Only add a type of the list if it's a Control, a concrete class,
// and public.
if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic)
{
derivedTypes.Add(type);
}
}
// Sort the types. The custom TypeComparer class orders types
// alphabetically by type name.
derivedTypes.Sort(new AlphabeticalComparer());
// Show the list of types.
lstTypes.ItemsSource = derivedTypes;
}
private void lstTypes_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
try
{
// Get the selected type.
Type type = (Type)lstTypes.SelectedItem;
// Instantiate the type.
ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes);
Control control = (Control)info.Invoke(null);
// Add it to the grid (but keep it hidden).
control.Visibility = Visibility.Collapsed;
layoutRoot.Children.Add(control);
// Get the template.
ControlTemplate template = control.Template;
// Get the XAML for the template.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
XamlWriter.Save(template, writer);
// Display the template.
txtTemplate.Text = sb.ToString();
// Remove the control from the grid.
layoutRoot.Children.Remove(control);
}
catch (Exception err)
{
txtTemplate.Text = "<< テンプレートの生成に失敗しました。: " + err.Message + ">>";
}
}
private class AlphabeticalComparer
: IComparer<Type>
{
public int Compare(Type x, Type y)
{
return string.Compare(x.FullName, y.FullName);
}
}
}
}
|