ListViewItem不会延伸到ListView的宽度


81

我目前正在设计使用XAML的Windows 8应用商店应用程序,但是我有一个较小的大小问题。我有一个带有DataTemple的ListView。

我的ListView和DataTemplate的代码如下:

<ListView x:Name="listPageItems"
          Grid.Row="1"
          SelectionMode="Extended"
          IsSwipeEnabled="False"
          ItemsSource="{Binding Mode=OneWay, Source={StaticResource items}}"
          ItemTemplate="{StaticResource NavigationItemTemplate}"
          ScrollViewer.VerticalScrollBarVisibility="Visible">

</ListView>

<DataTemplate x:Key="NavigationItemTemplate">    
        <Grid Height="75">
            <Grid.RowDefinitions>
                <RowDefinition Height="1.6*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Rectangle Fill="White" />
            <Rectangle Fill="{StaticResource SSEGreenBrush}"
                       Grid.Row="1" />
            <Border BorderThickness="2"
                    BorderBrush="{StaticResource SSEGreenBrush}"
                    Grid.RowSpan="2" />
            <TextBlock x:Name="textTitle"
                       Text="{Binding ClientName}"
                       Style="{StaticResource TitleTextStyle}"
                       Foreground="{StaticResource SSEBlueBrush}"
                       Margin="10,5,5,5" />
            <StackPanel Orientation="Horizontal"
                        Grid.Row="1"
                        HorizontalAlignment="Stretch">
                <TextBlock Text="Last Edit :"
                           Style="{StaticResource SubtitleTextStyle}"
                           Foreground="{StaticResource SSEBlueBrush}"
                           Margin="3,0,0,3"
                           VerticalAlignment="Center" />
                <TextBlock Text="SurveyDate"
                           Style="{StaticResource SubtitleTextStyle}"
                           Foreground="{StaticResource SSEBlueBrush}"
                           Margin="3,0,0,3"
                           VerticalAlignment="Center" />
            </StackPanel>
        </Grid>
    </DataTemplate>

列表视图位于固定宽度为240的网格列中。

显示视图时,ListViewItems不会延伸到ListView的宽度。我尝试设置许多属性,包括Horizo​​ntalContentAlignment,但似乎无法让ListViewItem伸展!

有人可以帮忙吗?

提前致谢。

我正在使用Visual Studio 2012,C#4.5和开发Windows应用商店。

Answers:


197

尝试将以下内容添加到ListView定义中

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>

1
错误:BindingExpression路径错误:在Project.CustomElement,Project.WindowsPhone,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null上找不到'Width'属性。BindingExpression:Path ='Width'DataItem = Project.CustomElement,Project.WindowsPhone,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'; 目标元素是“ Windows.UI.Xaml.Controls.Grid”(名称=“空”);目标属性为“宽度”(类型为“双
精度

2
例如,在DataTemplate中不会像网格那样自动拉伸到全宽的元素上,您必须另外设置Horizo​​ntalAlignment =“ Stretch”
tipa 15'Nov

我还想添加一条评论,当我还设置了Horizo​​ntalAlignment =“ Left”时,我无法使它起作用,但是当我删除它时,它就像一种魅力!
AndreasForslöw
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.