Silverlight 4 - 简单的控制模板

时间:2010-10-21 20:13:58

标签: silverlight

    <DataTemplate x:Key="dirtSimple">
        <TextBlock Margin="10,0,0,0" Text="{Binding Path=CurrentBook.Published, StringFormat=d}"></TextBlock>
    </DataTemplate>

    <ControlTemplate x:Key="lbWrapPanelTemplate">
        <StackPanel Orientation="Horizontal" Margin="2" Background="Aqua">
            <ItemsPresenter></ItemsPresenter>
        </StackPanel>
    </ControlTemplate>

...

    <ListBox Template="{StaticResource lbWrapPanelTemplate}" x:Name="bookListBox"  Grid.Row="0" ItemsSource="{Binding Path=BookSource}" ItemTemplate="{StaticResource dirtSimple}"  >
    </ListBox>

列表框显示正确,带有漂亮的“Aqua”背景,每个项目都只显示日期。出于某种原因虽然物品没有水平流动。我最初尝试使用Silverlight Toolkit的WrapPanel,遇到同样的问题,但我甚至无法使用内置的StackPanel,所以我怀疑我错过了什么。

1 个答案:

答案 0 :(得分:1)

您是否尝试获取ListBox提供的基于选择的行为?如果没有,请使用ItemsControl(并提供如下的ItemsPanel)。

它不是横向的原因是ItemsPresenter最终有自己的面板,它放置了项目。它没有将每个项目分别插入到你的StackPanel(或WrapPanel)中,而是将它们放在自己的面板中

您要做的是为ItemsPanel指定一个值,如下所示:

<ListBox ItemTemplate="{StaticResource dirtSimple}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>