9
我想要一個ItemsControl,其中的項目顯示爲水平。爲什麼我的ItemsControls中的項不能水平佈局?
然而,不管我用的StackPanel與方向=「橫向」或WrapPanel,他們仍然疊起來。
如何獲得ItemsControl中的項目是水平的?
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>
代碼隱藏:約
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; }
}
}
你會發幾次同樣的問題? – 2009-08-04 15:44:42
這必須是緊急的。 – Max 2009-08-04 15:48:25