因为我想用listView
来安排水平项目。因此,我将ItemsPanel
设置为StackPanel
,方向为水平。
<ListView x:Name="lv" ItemsSource="{Binding}" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="Salmon" VerticalAlignment="Top">
<ListView ItemsSource="{Binding Items}" Height="Auto">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StudentName}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
结果是这样的:
如何将它们设置为顶部对齐?
后台代码是:
namespace WpfApplication2
{
public partial class MainWindow : Window
{
ObservableCollection<Group> school;
public ObservableCollection<Group> Schools
{
get { return school; }
set { school = value; }
}
public MainWindow()
{
InitializeComponent();
Schools = new ObservableCollection<Group>();
Group g1 = new Group();
g1.Items = new ObservableCollection<Student>();
g1.Items.Add(new Student() { StudentName = "a" });
g1.Items.Add(new Student() { StudentName = "b" });
Schools.Add(g1);
Group g2 = new Group();
g2.Items = new ObservableCollection<Student>();
g2.Items.Add(new Student() { StudentName = "g2a" });
g2.Items.Add(new Student() { StudentName = "g2b" });
g2.Items.Add(new Student() { StudentName = "g2c" });
g2.Items.Add(new Student() { StudentName = "g2c" });
g2.Items.Add(new Student() { StudentName = "g2c" });
g2.Items.Add(new Student() { StudentName = "g2c" });
g2.Items.Add(new Student() { StudentName = "g2c" });
Schools.Add(g2);
lv.DataContext = Schools;
}
}
public class Group : ModelBase
{
private ObservableCollection<Student> item;
public ObservableCollection<Student> Items
{
get { return item; }
set { item = value; }
}
}
public class Student : ModelBase
{
private string sName;
public string StudentName
{
get { return sName; }
set { sName = value; }
}
}
public class ModelBase : INotifyPropertyChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public void Dispose()
{
this.OnDispose();
}
protected virtual void OnDispose()
{
}
}
}
答案 0 :(得分:0)
我认为您需要做的就是将ListView
包裹在网格中,并将网格的VerticalAlignment
设置为Top
。现在,窗口默认情况下垂直居中对齐其内容。
<Grid VerticalAlignment="Top">
<ListView x:Name="lv" ItemsSource="{Binding}" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="Salmon" VerticalAlignment="Top">
<ListView ItemsSource="{Binding Items}" Height="Auto">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StudentName}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
答案 1 :(得分:0)
<ListView.ItemContainerStyle>
<Style>
<Setter Property="Control.VerticalAlignment" Value="Top"/>
</Style>
</ListView.ItemContainerStyle>
答案 2 :(得分:0)
您可以通过ListView.ItemContainerStyle
属性(正如您在答案中指出的那样)实现它,但是有一种更短(更简单?)的方式 - 通过设置ListView.VerticalContentAlignment
:
<ListView VerticalContentAlignment="Top" (...)>
(...)
</ListView>
准确地说 - ListViewItem.VerticalAlignment
是通过绑定到最近ItemsControl.VerticalContentAlignment
祖先的ItemsControl
的默认样式 - 如果放置{ListViewItem
,您可以在输出窗口中观察到适当的错误消息任何ItemsControl
之外的{1}}。当然,水平对齐也类似。