2009-08-04 16 views
9

Desidero avere un comando Items in cui vengono visualizzati gli articoli in orizzontale.Perché gli articoli nel mio layout ItemsControls non sono orizzontali?

Tuttavia, non importa se uso StackPanel con Orientamento = "Orizzontale" o WrapPanel, ancora accumulano.

Come è possibile ottenere elementi in un ItemsControl in modo che siano orizzontali?

alt text http://i31.tinypic.com/dd2et5.png

XAML:

<Window x:Class="TestItemsControl2938.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window1" Height="300" Width="400"> 
    <Window.Resources> 
     <DataTemplate x:Key="CustomerListTemplate"> 
      <StackPanel Width="100" Background="#aaa" Margin="5"> 
       <TextBlock Text="{Binding LastName}"/> 
      </StackPanel> 
     </DataTemplate> 
    </Window.Resources>  
    <StackPanel> 
     <StackPanel Orientation="Horizontal" Background="Orange"> 
      <ItemsControl ItemsSource="{Binding CustomerList}" ItemTemplate="{StaticResource CustomerListTemplate}"/> 
     </StackPanel> 
     <WrapPanel Background="Yellow"> 
      <ItemsControl ItemsSource="{Binding CustomerList}" ItemTemplate="{StaticResource CustomerListTemplate}"/> 
     </WrapPanel> 
    </StackPanel> 
</Window> 

codice sottostante:

using System.Windows; 
using System.ComponentModel; 
using System.Collections.ObjectModel; 
namespace TestItemsControl2938 
{ 
    public partial class Window1 : Window, INotifyPropertyChanged 
    { 
     private ObservableCollection<Customer> _customerList = new ObservableCollection<Customer>(); 
     public ObservableCollection<Customer> CustomerList 
     { 
      get{ return _customerList; }  
      set 
      { 
       _customerList = value; 
       OnPropertyChanged("CustomerList"); 
      } 
     } 

     public Window1() 
     { 
      InitializeComponent(); 
      DataContext = this; 

      CustomerList.Add(new Customer { FirstName = "Jim", LastName = "Jones" }); 
      CustomerList.Add(new Customer { FirstName = "Joe", LastName = "Adams" }); 
      CustomerList.Add(new Customer { FirstName = "Jake", LastName = "Johnson" }); 
     } 


     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 

      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 

    public class Customer 
    { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string Street { get; set; } 
     public string Location { get; set; } 
     public string ZipCode { get; set; } 
    } 
} 
+1

quante volte invierai la stessa domanda? –

+0

Deve essere urgente. – Max

+0

Questa domanda è stata posta 3 volte. un po 'di confusione sono sicuro. Gli ID sono: 1228278 (questo), 1228270, 1228272. 1228272 è ora chiuso. Suggerisco di tenere aperto e chiudere il 1228270. – Cheeso

risposta

30

modo sbagliato. Personalizza il pannello che ItemsControl utilizza per contenere i suoi articoli:

<ItemsControl> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 
+0

Dove dovrei inserire ' '?? Mi sto legando a un oggetto e ho solo una lista orizzontale del mio tipo di oggetti. – Smithy

1

è necessario utilizzare il pannello degli articoli. guarda here la mia risposta

Problemi correlati