【问题标题】:Multiple text lines in a ListBox Item template?ListBox 项模板中有多个文本行?
【发布时间】:2013-04-27 17:53:10
【问题描述】:

我需要为我的 WP7 应用程序设置两个行列表框项目,其中一行是标题,然后是一个较小的副标题,其中包含一些细节。如何在 WP7 中做到这一点?

我正在使用 VS2010 和表达式混合来制作我的应用程序,目前我有一个用于单个列表框项目(文本)的自定义样式和项目模板。

这是我目前的代码

<phone:PhoneApplicationPage.Resources>
        <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
            <StackPanel Margin="0,20" HorizontalAlignment="Center">
                <StackPanel.Resources>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    </Style>
                </StackPanel.Resources>
            </StackPanel>
        </ItemsPanelTemplate>
        <Style x:Key="ListBoxStyle1" TargetType="ListBox">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBox">
                        <ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="#FF82C12E" Padding="{TemplateBinding Padding}" HorizontalAlignment="Center" FontSize="48">
                            <ItemsPresenter HorizontalAlignment="Center"/>
                        </ScrollViewer>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </phone:PhoneApplicationPage.Resources>

感谢您的帮助!

【问题讨论】:

    标签: silverlight windows-phone-7 xaml expression-blend listboxitem


    【解决方案1】:

    你可以有一个ItemTemplate:

      <DataTemplate x:Key="MyItemTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0"/>
                <TextBlock Grid.Row="1"/>
            </Grid>
        </DataTemplate>
    

    并在您的 ListBox 中绑定属性:

    <ListBox ItemTemplate="{StaticResource MyItemTemplate}"/>
    

    【讨论】:

    • 谢谢,我将如何填充列表框中的两个文本块?目前我正在使用字符串列表作为 itemsource
    • 如果你的 ItemSource 是一个 List 你只能绑定一个 TextBox 并且你会像这样绑定:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 2023-03-15
    • 2011-07-31
    • 1970-01-01
    • 2017-02-02
    相关资源
    最近更新 更多