拉伸视框内一个WPF的ListView项目 [英] Stretching the items in a WPF ListView within a ViewBox

查看:160
本文介绍了拉伸视框内一个WPF的ListView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个令人沮丧的问题,我会多AP preciate一些帮助。我有一个视框内的ListView和我不能在ListView中的项目水平拉伸。

I have a frustrating problem that I would much appreciate some help with. I have a ListView within a ViewBox and I can't get the items within the ListView to stretch horizontally.

下面是在 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>

下面是与皮肤和围垦对象的定义背后的code:

Here is the code behind with the definitions of the Skin and Inning objects:

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();
  }


 }
}

这可能发生在一局可以改变,而且可以由用户来改变,所以我把列表视图进入ViewBoxes让他们自动调整相应的皮肤时的数量改变皮肤的数量。由此产生的窗口可以在这里看到: http://i52.tinypic.com/244wqpl.jpg

The number of skins that can occur in an inning can change, and can be changed by the user, so I've put the ListViews into ViewBoxes so they automatically resize accordingly when the number of skins change. The resulting window can be seen here: http://i52.tinypic.com/244wqpl.jpg

我已经试过各种 HorzontalAlignment =拉伸 Horizo​​ntalContentAlignment =拉伸的组合和试图修改ItemsPanel模板,但我不是为我的生活似乎可以找出如何让ListView的在水平方向伸展。就是我试图做不可能没有一些code后面的改变动态ListView控件的宽度是多少?还是我失去了一些东西真的很简单吗?

I've tried all sorts of combinations of HorzontalAlignment="Stretch" and HorizontalContentAlignment="Stretch" and tried modifying the ItemsPanel template but I can't for the life of me seem to figure out how to get the ListView to stretch horizontally. Is what I'm trying to do impossible without some code behind to alter the width of the ListView dynamically? Or am I missing something really simple?

任何帮助,任何人都可以提供将是最AP preciated。

Any help that anyone can offer would be most appreciated.

谢谢,

推荐答案

尝试将ItemContainerStyle你的ListView的东西,如:

Try setting the ItemContainerStyle for your ListView to something like:

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

您可能还需要设置&LT; Viewbox控件拉伸=填充/方式&gt;

在此之后,我想你可以删除所有其他的Horizo​​ntalAlignment =拉伸和Horizo​​ntalContentAlignment =拉伸制定者在code,因为它可能会没有必要了。

After this, I think you can remove all those other "HorizontalAlignment = Stretch" and "HorizontalContentAlignment = Stretch" setters in your code since it probably won't be necessary anymore.

这篇关于拉伸视框内一个WPF的ListView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆