222

WPFのaControlTemplateとaの違いは何ですか?DataTemplate

4

7 に答える 7

290

通常、コントロールはそれ自体のためにレンダリングされ、基になるデータを反映しません。たとえば、 aButtonはビジネス オブジェクトにバインドされません。純粋にそこにあるため、クリックできます。ContentControlただし、またはは通常ListBox、ユーザーにデータを提示できるように表示されます。

したがってDataTemplate、 は基になるデータの視覚的な構造を提供するために使用されますが、 は基にControlTemplateなるデータとは関係なく、単にコントロール自体に視覚的なレイアウトを提供します。

通常、 には式ControlTemplateのみが含まTemplateBindingれ、コントロール自体のプロパティにバインドされますが、DataTemplateには標準のバインディング式が含まれ、そのプロパティDataContext(ビジネス/ドメイン オブジェクトまたはビュー モデル) にバインドされます。

于 2009-08-27T10:20:18.967 に答える
121

非常に基本的にはControlTemplate、コントロールを表示する方法をDataTemplate説明し、データを表示する方法を説明します。

例えば:

ALabelはコントロールであり、あるコンテンツ (または別のコントロール)の周りにa を使用して表示ControlTemplateする必要があることを示す a が含まれます。LabelBorderDataTemplate

Customerクラスは Data であり、 を使用して表示されます。これは、名前を表示する 1 つと電話番号を表示する2 つを含むとしてタイプDataTemplateを表示すると言うことができます。すべてのクラスが を使用して表示されることに注意してください。通常は、オブジェクトのメソッドの結果にプロパティが設定されたデフォルト テンプレートを使用します。CustomerStackPanelTextBlocksDataTemplatesTextBlockTextToString

于 2009-08-27T14:11:46.297 に答える
35

Troels LarsenがMSDN フォーラムで適切な説明をしています

<Window x:Class="WpfApplication7.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">
  <Window.Resources>
    <DataTemplate x:Key="ButtonContentTemplate">
      <StackPanel Orientation="Horizontal">
        <Grid Height="8" Width="8">
          <Path HorizontalAlignment="Stretch" 
           Margin="0,0,1.8,1.8" 
           VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FF000000" 
           Data="M0.5,5.7 L0.5,0.5 L5.7,0.5"/>
          <Path HorizontalAlignment="Stretch" 
           Margin="2,3,0,0" 
           VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FFFFFFFF" 
           Data="M3.2,7.5 L7.5,7.5 L7.5,3.5"/>
          <Path HorizontalAlignment="Stretch" 
           Margin="1.2,1.4,0.7,0.7" 
           VerticalAlignment="Stretch" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" 
           Data="M2.5,2.5 L7.5,7.5"/>
          <Path HorizontalAlignment="Stretch" 
           Margin="1.7,2.0,1,1" 
           VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FF000000" 
           Data="M3,7.5 L7.5,7.5 L7.5,3.5"/>
          <Path HorizontalAlignment="Stretch" 
           Margin="1,1,1,1" 
           VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FFFFFFFF" 
           Data="M1.5,6.5 L1.5,1 L6.5,1.5"/>
        </Grid>
        <ContentPresenter Content="{Binding}"/>
      </StackPanel>
    </DataTemplate>
    <ControlTemplate TargetType="Button" x:Key="ButtonControlTemplate">
      <Grid>
        <Ellipse Fill="{TemplateBinding Background}"/>
        <ContentPresenter HorizontalAlignment="Center"
              VerticalAlignment="Center"/>
      </Grid>
    </ControlTemplate>
  </Window.Resources>
  <StackPanel>
    <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="1"/>
    <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="2"/>
    <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="3"/>
  </StackPanel>
</Window>

(テンプレートは、 http : //msdn.microsoft.com/en-us/library/system.windows.controls.controltemplate.aspx および http://msdn.microsoft.com/en-us/library/system.windowsから露骨に盗まれました.controls.contentcontrol.contenttemplate%28VS.95%29.aspx それぞれ)

とにかく、ControlTemplate は Button 自体の外観を決定し、ContentTemplate はボタンの Content の外観を決定します。そのため、コンテンツをデータ クラスの 1 つにバインドし、必要に応じて表示することができます。

于 2012-11-18T17:29:57.487 に答える
23

ControlTemplate: コントロール スタイルを表します。

DataTemplate: データのスタイル (データをどのように表示したいか) を表します。

すべてのコントロールは、テンプレート プロパティを介してオーバーライドできる既定のコントロール テンプレートを使用しています。

たとえば、
Buttonテンプレートはコントロール テンプレートです。 Buttonコンテンツ テンプレートはデータ テンプレートです

<Button   VerticalAlignment="Top" >
    <Button.Template>
        <ControlTemplate >
            <Grid>
                <Rectangle Fill="Blue" RadiusX="20" RadiusY="20"/>
                <Ellipse Fill="Red" />
                <ContentPresenter Content="{Binding}">
                    <ContentPresenter.ContentTemplate>
                        <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="50">
                            <TextBlock Text="Name" Margin="5"/>
                                <TextBox Text="{Binding UserName, Mode=TwoWay}" Margin="5" Width="100"/>
                            <Button Content="Show Name" Click="OnClickShowName" />
                        </StackPanel>
                    </DataTemplate>
                    </ContentPresenter.ContentTemplate>
                </ContentPresenter>
            </Grid>
        </ControlTemplate>
    </Button.Template>
</Button>

public String UserName
{
    get { return userName; }
    set
    {
        userName = value;
        this.NotifyPropertyChanged("UserName");
    }
}
于 2012-11-07T19:26:35.297 に答える
8

ControlTemplate- 要素の外観を変更します。たとえばButton、画像とテキストを含めることができます

DataTemplate- 要素を使用して基になるデータを表す。

于 2011-03-27T16:51:56.327 に答える
1

ControlTemplate視覚的な外観を定義DataTemplateし、データ項目の視覚的な外観を置き換えます。

例: ボタンを長方形から円形に表示したい => コントロール テンプレート。

また、コントロールに複雑なオブジェクトがある場合はToString()、 を呼び出して表示するだけで、DataTemplateさまざまなメンバーを取得して、データ オブジェクトの値を表示および変更できます。

于 2014-08-04T04:20:40.157 に答える