Silverlight ItemsControl

时间:2009-11-24 03:19:59

标签: silverlight xaml silverlight-3.0 itemscontrol

我正在尝试使用ItemsControl来显示DataTemplate。我有这个简单的例子:

<navigation:Page.Resources>
    <DataTemplate x:Key="PictureResultsTemplate">
        <!--<Grid/> -->
        <TextBlock Text="Nick Was Here"></TextBlock>
    </DataTemplate>
</navigation:Page.Resources>       


<Grid x:Name="LayoutRoot">
    <Grid Margin="0,0,8,8">
        <Grid.RowDefinitions>
            <RowDefinition Height="0.102*"/>
            <RowDefinition Height="0.898*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

    <ItemsControl x:Name="PictureResults" Margin="0,8,0,0" Grid.Row="2" ItemTemplate="{StaticResource PictureResultsTemplate}">
    </ItemsControl>     

    </Grid>         

</Grid>

为什么文本块文本不可见?谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您需要在items控件上设置ItemsSource或Items,否则您将没有要将ItemTemplate应用到的项目。

不,您不必绑定到数据源。您可以直接在xaml中添加项目。见下文:

<UserControl
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="QuickTests.MainPage"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Width="640" Height="480">
    <UserControl.Resources>
        <DataTemplate x:Key="myStringTemplate">
            <StackPanel>
                <TextBlock Text="{Binding}"/>
                <TextBlock Text="Yep, this is an item"/>
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>
                  <ItemsControl ItemTemplate="{StaticResource myStringTemplate}">
                        <ItemsControl.Items>
                            <System:String>hello</System:String>
                            <System:String>world</System:String>
                        </ItemsControl.Items>
                    </ItemsControl>
    </UserControl>