模板设计问题

时间:2011-10-28 14:51:55

标签: c# silverlight windows-phone-7

我有一个App.xaml模板:

<Application.Resources>
        <!-- template for recent history -->
        <DataTemplate x:Key="ListViewModelTemplate"> <!-- for recent recepies-->

            <Grid Width="400" Height="80" VerticalAlignment="Center">

                <StackPanel Orientation="Vertical">
                <Border CornerRadius="0" x:Name="brdTesat" BorderBrush="Black" BorderThickness="1" Width="80" Height="80">

                <Border.Background>
                    <ImageBrush x:Name="backgroundImaageBrush" Stretch="Fill">

                        <ImageBrush.ImageSource>

                                <BitmapImage x:Name="bmapBackground" UriSource="{Binding imageUriPath}" >
                            </BitmapImage>

                        </ImageBrush.ImageSource>
                    </ImageBrush>
                </Border.Background>
            </Border>
                <StackPanel>
                <TextBlock TextAlignment="Left" Margin="7,4,4,4" Text="{Binding title}" TextWrapping="Wrap"></TextBlock>
                    <TextBlock TextAlignment="Left" Margin="7,4,4,4" Text="{Binding subTitle}" TextWrapping="Wrap"></TextBlock>
                </StackPanel>
            </StackPanel>

        </Grid>
    </DataTemplate>

并尝试将其调用以显示在ListBox中。问题是列表框虽然绑定到数据,但不知道如何使用模板。这是我的列表框定义:

<ListBox x:Name="recepiesList"  ItemsSource="{Binding recepiesList}" ItemTemplate="{StaticResource ListViewModelTemplate}"  >

如果我定义了一个模板,比如

<ListBox.Template><DataTemplate><TextBlock text={Binding title} /></DataTemplate></ListBox.Template>

列表框效果很好,但我需要更正我的Application.Resources。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

这是正确的DataTemplate

 <DataTemplate x:Key="ListViewModelTemplate"> <!-- for recent recepies-->

                <!-- for recent recepies-->
                <StackPanel Orientation="Horizontal">
                    <Border CornerRadius="0" x:Name="brdTesat" BorderBrush="White" BorderThickness="1" Width="80" Height="80">

                        <Border.Background>
                            <ImageBrush x:Name="backgroundImaageBrush" Stretch="Fill">

                                <ImageBrush.ImageSource >

                                    <BitmapImage x:Name="bmapBackground"  UriSource="{Binding imageUriPath}" >
                                    </BitmapImage>

                                </ImageBrush.ImageSource>
                            </ImageBrush>
                        </Border.Background>
                    </Border>
                    <StackPanel >
                        <TextBlock TextAlignment="Left" Margin="7,4,4,4" Text="{Binding title}" TextWrapping="Wrap"></TextBlock>
                        <TextBlock TextAlignment="Left" Margin="7,4,4,4" Text="{Binding subTitle}" TextWrapping="Wrap"></TextBlock>
                    </StackPanel>
                </StackPanel>


            </DataTemplate>

网格似乎产生了一个问题..