【问题标题】:Listbox items not filling the entire width of screen WP7列表框项目未填满屏幕 WP7 的整个宽度
【发布时间】:2013-04-25 21:41:16
【问题描述】:

我正在尝试使用以下模板创建一个包含项目的列表框

 <ListBox x:Name="moviesactedin" Style="{StaticResource ListBoxStyle2}">
       <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="0,0,0,1" BorderBrush="Black" Margin="0,0,0,5">
                   <StackPanel Orientation="Horizontal" Margin="0,0,0,5" HorizontalAlignment="Stretch">
                         <Image Source="{Binding Image}" Stretch="None" Margin="0,0,5,5" />
                         <StackPanel Orientation="Vertical" Margin="10,0,0,0" VerticalAlignment="Top">
                        <TextBlock Text="{Binding Title}" Foreground="Black" FontWeight="Bold"/>
                        <TextBlock Text="{Binding Director}" Foreground="Black"/>
                        <TextBlock Text="{Binding ReleaseDate}" Foreground="Black"/>
                  </StackPanel>
                      <Image Source="/Images/icon_arrow.png" HorizontalAlignment="Right" VerticalAlignment="Center"/>
                   </StackPanel>

              </Border>
        </DataTemplate>
</ListBox.ItemTemplate>

和列表框样式

<Style x:Key="ListBoxStyle2" 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="Stretch" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
              <ControlTemplate TargetType="ListBox">
                        <ItemsPresenter  HorizontalAlignment="Stretch" 
                                            VerticalAlignment="Stretch"/>
               </ControlTemplate>
         </Setter.Value>
    </Setter>
</Style>

第一个图像将在左侧,带有三个文本块的 stackPanel 将在中间,第二个图像将在右侧(最后)。

问题是列表框项没有填满屏幕的整个宽度,因此我将最后一张图像填满屏幕宽度。

有什么帮助吗?

【问题讨论】:

  • 你检查了父页边距吗?
  • 父级是一个边距设置为0,0,0,0的网格

标签: c# windows windows-phone-7


【解决方案1】:

成功了

<ListBox.ItemContainerStyle>
         <Style TargetType="ListBoxItem">
               <Setter Property="Template">
                      <Setter.Value>
                           <ControlTemplate>
                                   <ContentPresenter
                                        HorizontalAlignment="Stretch" 
                                       VerticalAlignment="Stretch"
                                   />
                          </ControlTemplate>
                      </Setter.Value>
               </Setter>
           </Style>
</ListBox.ItemContainerStyle>

项目填满屏幕的整个长度。感谢 Fabrice 提供的线索。

【讨论】:

    【解决方案2】:

    你可以试试吗?

    <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/></Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-21
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      相关资源
      最近更新 更多