locked
ControlTemplate 和ItemTemplate 有什么区别啊 RRS feed

  • 问题

  • ControlTemplate 和ItemTemplate 有什么区别啊
    我看有些控件要用 ControlTemplate 自定内外观 有些需要ItemTemplate  定义外观
    2009年7月3日 7:22

答案

  • 好像在csdn回复过同样的问题,我把答案粘过来了,有不确切的地方,请高手指正。


    silverlight的控件大概派生于两种类。一种是contentcontrol类(例如按钮控件),另一种是itemscontrol(例如listbox控件,这种控件可以实现项目集合)。由于contentcontrol类提供了contenttemplate属性,所以可以采用controltemplate自定义控件的内容.如下代码
    <Button>
            <Button.Template>
                <ControlTemplate>
                    <Grid>
                        <Rectangle Fill="LightBlue" Stroke="Black" StrokeThickness="5"
                                  RadiusX="20" RadiusY="20"/>
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
                            I'm a CT
                        </TextBlock>
                    </Grid>
                </ControlTemplate>
            </Button.Template>
        </Button>

    对于listbox这类控件可以使用itemtemplate来自定义控件的外观
    使用 ItemTemplate 属性来指定数据的直观表示形式。同时使用 DataTemplate 来定义数据对象的外观。例如下面代码:
    <ListBox ItemsSource="{StaticResource customers}" Width="350" Margin="0,5,0,10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Padding="5,0,5,0"
                  Text="{Binding FirstName}" />
                        <TextBlock Text="{Binding LastName}" />
                        <TextBlock Text=", " />
                        <TextBlock Text="{Binding Address}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    2009年7月3日 9:36
  • ControlTemplate是指控件的样式
    ItemTemplate是指可绑定泛型的控件内容的样式

    即只有那些有item的控件才有ItemTemplate(如ListBox ,Combox,TreeView,DataGrid,TabelControl等,),但是所有控件都有ControlTemplate

    2009年7月3日 12:28

全部回复

  • 好像在csdn回复过同样的问题,我把答案粘过来了,有不确切的地方,请高手指正。


    silverlight的控件大概派生于两种类。一种是contentcontrol类(例如按钮控件),另一种是itemscontrol(例如listbox控件,这种控件可以实现项目集合)。由于contentcontrol类提供了contenttemplate属性,所以可以采用controltemplate自定义控件的内容.如下代码
    <Button>
            <Button.Template>
                <ControlTemplate>
                    <Grid>
                        <Rectangle Fill="LightBlue" Stroke="Black" StrokeThickness="5"
                                  RadiusX="20" RadiusY="20"/>
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
                            I'm a CT
                        </TextBlock>
                    </Grid>
                </ControlTemplate>
            </Button.Template>
        </Button>

    对于listbox这类控件可以使用itemtemplate来自定义控件的外观
    使用 ItemTemplate 属性来指定数据的直观表示形式。同时使用 DataTemplate 来定义数据对象的外观。例如下面代码:
    <ListBox ItemsSource="{StaticResource customers}" Width="350" Margin="0,5,0,10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Padding="5,0,5,0"
                  Text="{Binding FirstName}" />
                        <TextBlock Text="{Binding LastName}" />
                        <TextBlock Text=", " />
                        <TextBlock Text="{Binding Address}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    2009年7月3日 9:36
  • ControlTemplate是指控件的样式
    ItemTemplate是指可绑定泛型的控件内容的样式

    即只有那些有item的控件才有ItemTemplate(如ListBox ,Combox,TreeView,DataGrid,TabelControl等,),但是所有控件都有ControlTemplate

    2009年7月3日 12:28
  • 好像在csdn回复过同样的问题,我把答案粘过来了,有不确切的地方,请高手指正。


    silverlight的控件大概派生于两种类。一种是contentcontrol类(例如按钮控件),另一种是itemscontrol(例如listbox控件,这种控件可以实现项目集合)。由于contentcontrol类提供了contenttemplate属性,所以可以采用controltemplate自定义控件的内容.如下代码
    <Button>
            <Button.Template>
                <ControlTemplate>
                    <Grid>
                        <Rectangle Fill="LightBlue" Stroke="Black" StrokeThickness="5"
                                  RadiusX="20" RadiusY="20"/>
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
                            I'm a CT
                        </TextBlock>
                    </Grid>
                </ControlTemplate>
            </Button.Template>
        </Button>

    对于listbox这类控件可以使用itemtemplate来自定义控件的外观
    使用 ItemTemplate 属性来指定数据的直观表示形式。同时使用 DataTemplate 来定义数据对象的外观。例如下面代码:
    <ListBox ItemsSource="{StaticResource customers}" Width="350" Margin="0,5,0,10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Padding="5,0,5,0"
                  Text="{Binding FirstName}" />
                        <TextBlock Text="{Binding LastName}" />
                        <TextBlock Text=", " />
                        <TextBlock Text="{Binding Address}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>


    支持.
    2009年7月7日 16:13