2010-09-15 16 views
15

Bazı yardımları çok takdir edeceğim sinir bozucu bir sorun var. ViewBox içinde bir ListView var ve ListView içindeki öğeleri yatay olarak germek için alamıyorum. İşte Bir ViewBox içinde bir WPF ListView öğelerinde öğeler Germe

<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> 

Deri ve İnning nesnelerin tanımları ile arkasında kodudur: Burada

XAML olduğunu

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


} 
} 

bir atış oluşabilir derileri sayısı değiştirebilir ve kullanıcı tarafından değiştirilebilir, bu yüzden ListViews ViewBoxes içine koydum böylece ciltlerin sayısı değiştiğinde otomatik olarak yeniden boyutlandırır. Ortaya çıkan pencere burada görülebilir: http://i52.tinypic.com/244wqpl.jpg

Ben HorzontalAlignment="Stretch" ve HorizontalContentAlignment="Stretch" kombinasyonları her türlü denedim ve ItemsPanel şablonu değiştirerek denedim ama benim yaşam için ListView nasıl anlamaya görünüyor olamaz yatay olarak germek. ListView'in genişliğini dinamik olarak değiştirmek için bazı kodlar olmadan imkansız hale getirmek istediğim şey nedir? Yoksa gerçekten basit bir şeyi mi özlüyorum?

Herkesin sunabileceği herhangi bir yardım en çok takdir edilecektir.

sayesinde

Matta

cevap

50

böyle bir şey için ListView ItemContainerStyle belirlemeyi deneyin: <Viewbox Stretch="Fill"/> ayarlamak için de gerekebilir

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

.

Bundan sonra, muhtemelen artık gerekli olmayacağından, diğer tüm "HorizontalAlignment = Stretch" ve "HorizontalContentAlignment = Stretch" ayarlayıcılarını kaldırabilirsiniz.

+1

'Stretch = "Fill"' !! Bunun için çok teşekkür ederim. Görüntüleme kutusu uzatılmış ve ListViewItem.HorizontalContentAlignment, Stretch olarak ayarlanmış, yakındı. Bu verdi: http://i55.tinypic.com/28cdmwh.jpg - yakın, ancak metin uzatılmış sadece TextBlock yerine uzatılır. ListView'lerin genişliğini ViewBox'ın ActualWidth değerine bu kod aracılığıyla da ayarladıysam: 'Width =" {Binding RelativeSource = {RelativeSource FindAncestor, AncestorType = {x: Type Viewbox}}, Path = ActualWidth} "' tam olarak ne olduğumu. Bu: http://i54.tinypic.com/2mo8qk5.jpg Yardımlarınız için çok teşekkür ederim. – MajorRefactoring

+0

Bunun için teşekkürler. Bu benim için de bir liste kutusunda çalıştı. – MikeKulls

+1

Bir Windows Mağazası Uygulaması'nda benim için çalışmadı. Nesnenin, yazı tipinin, yüksekliğin ve genişliğin ölçek dönüşümüne neden olur. Ancak orijinal cevap beklendiği gibi çalışıyordu. –

5

Beklenmedik ScrollViewer.HorizontalScrollBarVisibility="Disabled" ayarı da benim için çalıştı:

<ListView ItemsSource="{Binding SourceList}" 
    ScrollViewer.HorizontalScrollBarVisibility="Disabled">