Xaml-templates

提供:Dev Guides
移動先:案内検索

XAML-テンプレート

テンプレートは、コントロールの全体的な外観と外観を記述します。 各コントロールには、そのコントロールに外観を与えるデフォルトのテンプレートが関連付けられています。

XAMLでは、コントロールの視覚的な動作と視覚的な外観をカスタマイズするときに、独自のテンプレートを簡単に作成できます。 ロジックとテンプレート間の接続は、データバインディングによって実現できます。

スタイルとテンプレートの主な違いは次のとおりです-

  • スタイルは、そのコントロールのデフォルトプロパティを持つコントロールの外観のみを変更できます。
  • テンプレートを使用すると、スタイルよりもコントロールの多くの部分にアクセスできます。 コントロールの既存の動作と新しい動作の両方を指定することもできます。

最も一般的に使用されるテンプレートには2つのタイプがあります。

  • 制御テンプレート
  • データテンプレート

制御テンプレート

コントロールテンプレートは、コントロールの視覚的な外観と構造を定義または指定します。 UI要素はすべて、何らかの外観と動作を持っています。たとえば、Buttonには外観と動作があります。 クリックイベントまたはマウスホバーイベントは、クリックアンドホバーに応答して発生する動作であり、コントロールテンプレートによって変更できるボタンのデフォルトの外観もあります。

いくつかのプロパティを持つ2つのボタンが作成される簡単な例をもう一度見てみましょう。 1つは template を使用し、もう1つは default ボタンを使用します。

<Window x:Class = "TemplateDemo.MainWindow"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   Title = "MainWindow" Height = "350" Width = "604">

   <Window.Resources>
      <ControlTemplate x:Key = "ButtonTemplate" TargetType = "Button">
         <Grid>
            <Ellipse x:Name = "ButtonEllipse" Height = "100" Width = "150" >
               <Ellipse.Fill>
                  <LinearGradientBrush StartPoint = "0,0.2" EndPoint = "0.2,1.4">
                     <GradientStop Offset = "0" Color = "Red"/>
                     <GradientStop Offset = "1" Color = "Orange"/>
                  </LinearGradientBrush>
               </Ellipse.Fill>
            </Ellipse>
            <ContentPresenter Content = "{TemplateBinding Content}"
               HorizontalAlignment = "Center" VerticalAlignment = "Center"/>
         </Grid>
         <ControlTemplate.Triggers>
            <Trigger Property = "IsMouseOver" Value = "True">
               <Setter TargetName = "ButtonEllipse" Property = "Fill" >
                  <Setter.Value>
                     <LinearGradientBrush StartPoint = "0,0.2" EndPoint="0.2,1.4">
                        <GradientStop Offset = "0" Color = "YellowGreen"/>
                        <GradientStop Offset = "1" Color = "Gold"/>
                     </LinearGradientBrush>
                  </Setter.Value>
               </Setter>
            </Trigger>

            <Trigger Property = "IsPressed" Value = "True">
               <Setter Property = "RenderTransform">
                  <Setter.Value>
                     <ScaleTransform ScaleX = "0.8" ScaleY = "0.8" CenterX = "0" CenterY = "0"/>
                  </Setter.Value>
               </Setter>

               <Setter Property = "RenderTransformOrigin" Value = "0.5,0.5"/>
            </Trigger>
         </ControlTemplate.Triggers>
      </ControlTemplate>
   </Window.Resources>

   <StackPanel>
      <Button Content = "Round Button!" Template = "{StaticResource ButtonTemplate}"
         Width = "150" Margin = "50"/>
      <Button Content = "Default Button!" Height = "40" Width = "150" Margin = "5"/>
   </StackPanel>

</Window>

上記のコードがコンパイルおよび実行されると、次のMainWindowが生成されます-

コントロールテンプレート

あなたがカスタムテンプレートを使用してボタンの上にマウスを置くと、次に示すように色も変わります-

Control Template1

データテンプレート

データテンプレートは、データコレクションの外観と構造を定義および指定します。 任意のUI要素でデータのプレゼンテーションをフォーマットおよび定義する柔軟性を提供します。 主に、ComboBox、ListBoxなどのデータ関連のItemコントロールで使用されます。

データテンプレートの簡単な例を見てみましょう。 次のXAMLコードは、データテンプレートとテキストブロックを含むコンボボックスを作成します。

<Window x:Class = "XAMLDataTemplate.MainWindow"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   Title = "MainWindow" Height = "350" Width = "604">

   <Grid VerticalAlignment = "Top">
      <ComboBox Name = "Presidents" ItemsSource = "{Binding}" Height = "30" Width = "400">
         <ComboBox.ItemTemplate>
            <DataTemplate>
               <StackPanel Orientation = "Horizontal" Margin = "2">
                  <TextBlock Text = "Name: " Width = "95" Background = "Aqua" Margin = "2"/>
                  <TextBlock Text = "{Binding Name}" Width = "95" Background = "AliceBlue" Margin = "2"/>
                  <TextBlock Text = "Title: " Width = "95" Background = "Aqua" Margin = "10,2,0,2"/>
                  <TextBlock Text = "{Binding Title}" Width = "95" Background = "AliceBlue" Margin = "2"/>
               </StackPanel>
            </DataTemplate>
         </ComboBox.ItemTemplate>
      </ComboBox>
   </Grid>

</Window>

これは、従業員オブジェクトがDataContextに割り当てられているC#の実装です-

using System;
using System.Windows;
using System.Windows.Controls;

namespace XAMLDataTemplate {
  ///<summary>
     ///Interaction logic for MainWindow.xaml
  ///</summary>

   public partial class MainWindow : Window {
      public MainWindow() {
         InitializeComponent();
         DataContext = Employee.GetEmployees();
      }
   }
}

これは、EmployeeクラスのC#での実装です-

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace XAMLDataTemplate {
   public class Employee : INotifyPropertyChanged {
      private string name; public string Name {
         get { return name; }
         set { name = value; RaiseProperChanged(); }
      }
      private string title; public string Title {
         get { return title; }
         set { title = value; RaiseProperChanged(); }
      }
      public static Employee GetEmployee() {
         var emp = new Employee() {
            Name = "Waqas", Title = "Software Engineer" };
         return emp;
      }
      public event PropertyChangedEventHandler PropertyChanged;
      private void RaiseProperChanged( [CallerMemberName] string caller = ""){
         if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
         }
      }
      public static ObservableCollection<Employee> GetEmployees() {
         var employees = new ObservableCollection<Employee>();
         employees.Add(new Employee() { Name = "Ali", Title = "Developer" });
         employees.Add(new Employee() { Name = "Ahmed", Title = "Programmer" });
         employees.Add(new Employee() { Name = "Amjad", Title = "Desiner" });
         employees.Add(new Employee() { Name = "Waqas", Title = "Programmer" });
         employees.Add(new Employee() { Name = "Bilal", Title = "Engineer" });
         employees.Add(new Employee() { Name = "Waqar", Title = "Manager" });
         return employees;
      }
   }
}

上記のコードをコンパイルして実行すると、次の出力が生成されます。 コンボボックスが含まれており、コンボボックスをクリックすると、Employeeクラスで作成されたデータのコレクションがコンボボックスアイテムとしてリストされていることがわかります。

データテンプレート

上記のコードを実行して試してみることをお勧めします。