最終更新:2020-04-06 (月) 17:37:56 (1473d)  

WPF/テンプレート
Top / WPF / テンプレート

用語

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

  • コントロールの外観を自由自在にカスタマイズ可能
  • Control.Template (型:System.Windows.Controls.ControlTemplate)
    <Button>
      <Button.Template>
        <ControlTemplate TargetType="Button">
          <Ellipse Fill="LightBlue" Width="80" Height="30"/>
        </ControlTemplate>
      </Button.Template>
    </Button>
  • TargetType: テンプレートを適用するコントロールの型

データテンプレート

種類

Control.Template (型:System.Windows.Controls.ControlTemplate)

  • コントロール自体のカスタマイズ
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <Border BorderThickness="1"
                    BorderBrush="Black"
                    Background="Azure">
                <ItemsPresenter Margin="10" />
            </Border>
        </ControlTemplate>
    </ItemsControl.Template>

ItemsControl.ItemsPanel (型:System.Windows.Controls.ItemsPanelTemplate)

  • コレクション項目をどのようにレイアウトするかを決定
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <!-- (1) <StackPanel Orientation="Vertical" /> --> 
            <!-- (2) <WrapPanel Orientation="Horizontal" /> -->
            <!-- (3) <Grid /> -->
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

ItemsControl.ItemContainerStyle?

  • コレクションのコンテナのカスタマイズ
  • ListBoxが生成するコンテナー要素はListBoxItem?
  • ComboBoxが生成するコンテナー要素はComboBoxItem?

ItemsControl.ItemTemplate (型:System.Windows.DataTemplate)

  • 項目ごとの外観のカスタマイズ
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Margin="5">
                <Run Text="No." />
                <Run Text="{Binding Number}" />
                <LineBreak />
                <Run Text="Name:" />
                <Run Text="{Binding Name}" FontSize="15" />
            </TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

参考

関連