26

我有一个令人沮丧的问题,我非常感谢一些帮助。我在 ViewBox 中有一个 ListView,我无法让 ListView 中的项目水平拉伸。

这是XAML

<Window x:Class="WpfApplication3.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication3"
        Title="Window1">
    <Window.Resources>
        <local:Inning x:Key="inning">
            <local:Inning.Skins>
                <local:Skin SkinNumber="1"></local:Skin>
                <local:Skin SkinNumber="2"></local:Skin>
                <local:Skin SkinNumber="3"></local:Skin>
            </local:Inning.Skins>
        </local:Inning>
    </Window.Resources>
    <Grid DataContext="{StaticResource inning}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock Background="Black"
                   Text="SKINS"
                   Foreground="White"
                   FontSize="80"
                   FontWeight="Bold"
                   HorizontalAlignment="Center"></TextBlock>
        <Grid Margin="2"
              Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Viewbox>
                <ListView Name="lvSkinNumbers"
                          ItemsSource="{Binding Skins}"
                          HorizontalAlignment="Stretch"
                          Background="Black"
                          VerticalAlignment="Stretch"
                          VerticalContentAlignment="Stretch">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock FontSize="250"
                                       VerticalAlignment="Stretch"
                                       LineStackingStrategy="BlockLineHeight"
                                       Margin="2"
                                       TextAlignment="Center"
                                       HorizontalAlignment="Stretch"
                                       Background="Black"
                                       Foreground="White"
                                       Text="{Binding SkinNumber}"></TextBlock>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Viewbox>
            <Viewbox Grid.Column="1">
                <ListView Name="lvFirstInningSkins"
                          ItemsSource="{Binding Skins}"
                          Grid.Column="1"
                          HorizontalContentAlignment="Stretch"
                          Background="Black">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock FontSize="250"
                                       VerticalAlignment="Stretch"
                                       LineStackingStrategy="BlockLineHeight"
                                       Margin="2"
                                       TextAlignment="Center"
                                       HorizontalAlignment="Stretch"
                                       Background="Green"
                                       Foreground="White"
                                       Text="{Binding SkinNumber}"></TextBlock>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Viewbox>
            <Viewbox Grid.Column="2"
                     HorizontalAlignment="Stretch">
                <ListView Name="lvSecondInningSkins"
                          ItemsSource="{Binding Skins}"
                          Grid.Column="2"
                          HorizontalAlignment="Stretch"
                          Background="Black">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock FontSize="250"
                                       VerticalAlignment="Stretch"
                                       LineStackingStrategy="BlockLineHeight"
                                       Margin="2"
                                       TextAlignment="Center"
                                       HorizontalAlignment="Stretch"
                                       Background="Green"
                                       Foreground="White"
                                       Text="{Binding SkinNumber}"></TextBlock>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Viewbox>
        </Grid>
    </Grid>
</Window>

以下是 Skin 和 Inning 对象定义的代码:

using System;
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WpfApplication3
{

 public class Inning
 {
  private ObservableCollection<Skin> _skins = new ObservableCollection<Skin>();
  public ObservableCollection<Skin> Skins
  {
   get { return _skins; }
   set { _skins = value; }
  }
 }

 public class Skin : INotifyPropertyChanged
 {
  public Skin()
  {
  }

  public Skin( int skinNumber, Inning inning )
  {
   this.SkinNumber = skinNumber;
   this.Inning = inning;
  }

  public Inning Inning { get; set; }
  public int SkinNumber { get; set; }

  public int SkinCount
  {
   get { return this.Inning.Skins.Count; }
  }

  public void Notify( string propertyName )
  {
   if ( PropertyChanged != null )
    PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
  }

  public event PropertyChangedEventHandler PropertyChanged;

 }


 public partial class Window1 : Window
 {

  public Window1()
  {
   InitializeComponent();
  }


 }
}

一局中可能出现的皮肤数量可以更改,并且可以由用户更改,因此我将 ListViews 放入 ViewBoxes 中,以便在皮肤数量更改时它们会自动调整大小。生成的窗口可以在这里看到:http: //i52.tinypic.com/244wqpl.jpg

我已经尝试了各种组合,HorzontalAlignment="Stretch"HorizontalContentAlignment="Stretch"尝试修改 ItemsPanel 模板,但我一生似乎无法弄清楚如何让 ListView 水平拉伸。如果没有一些代码来动态改变 ListView 的宽度,我想要做的事情是不可能的吗?还是我错过了一些非常简单的东西?

任何人都可以提供的任何帮助将不胜感激。

谢谢,

马修

4

2 回答 2

67

尝试将 ListView 的 ItemContainerStyle 设置为:

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

您可能还需要设置<Viewbox Stretch="Fill"/>.

在此之后,我认为您可以删除代码中的所有其他“Horizo​​ntalAlignment = Stretch”和“Horizo​​ntalContentAlignment = Stretch”设置器,因为它可能不再需要了。

于 2010-09-15T10:31:47.557 回答
6

出乎意料的设置ScrollViewer.HorizontalScrollBarVisibility="Disabled"也对我有用:

<ListView ItemsSource="{Binding SourceList}"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled">
于 2015-06-09T09:17:18.447 回答