2009-08-04 19 views
9

Quiero tener un ItemsControl en el que los elementos se muestran horizontalmente.¿Por qué los elementos en mis elementos no controlan el diseño horizontalmente?

Sin embargo, no importa si uso StackPanel con Orientación = "horizontal" o WrapPanel, todavía se apilan.

¿Cómo puedo obtener elementos en un ItemsControl horizontalmente?

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> 

de código subyacente:

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

¿Cuantas veces enviarás la misma pregunta? –

+0

Eso debe ser urgente. – Max

+0

Esta pregunta ha sido hecha 3 veces. un poco de confusión, estoy seguro. Los ID son: 1228278 (este), 1228270, 1228272. 1228272 ahora está cerrado. Sugiero mantener este abierto, y cerrando 1228270. – Cheeso

Respuesta

30

revés. Personalice el panel que utiliza ItemsControl para contener sus elementos:

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

¿Dónde pondré ' '?? Me estoy vinculando a un objeto y solo tengo una lista horizontal de mi tipo de objetos. – Smithy

1

debe usar el panel de elementos. mira here mi respuesta

Cuestiones relacionadas