为ListView的项目模板定义边框

时间:2015-10-07 09:24:56

标签: c# wpf xaml listview

这是ListView的XAML代码。

<ListView VerticalAlignment="Stretch"
          HorizontalAlignment="Stretch"
          x:Name="listView1">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border HorizontalAlignment="Stretch"
                    BorderThickness="1"
                    BorderBrush="LightBlue">
                <StackPanel Orientation="Vertical">
                    <Label BorderThickness="0,0,0,1"
                           BorderBrush="#BABABA"
                           HorizontalAlignment="Stretch">Keywords</Label>
                    <TextBlock Text="{Binding keywords}"
                               Foreground="#3399AA"
                               HorizontalAlignment="Stretch" />
                    <Label BorderThickness="0,0,0,1"
                           BorderBrush="#BABABA">Content</Label>
                    <TextBlock MaxHeight="50"
                               Text="{Binding contentAsString}"
                               TextWrapping="Wrap"
                               Foreground="Black"
                               Height="auto"
                               Width="auto" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

以下是它的外观

...

无论我尝试过什么,我都无法为整个listview项创建边框。我们怎么能这样做?

1 个答案:

答案 0 :(得分:1)

尝试使用Stretch

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

。 。 。

<Viewbox Stretch="Fill"/>