当我将ListViewItem排列为水平时,项目在VerticalAlignment上排列中心,如何设置顶部VerticalAlignment?

时间:2016-06-14 02:59:26

标签: c# wpf listview

因为我想用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>

结果是这样的:

Sample Image

如何将它们设置为顶部对齐?

后台代码是:

            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()
                    {

                    }
                }
            }

3 个答案:

答案 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)

好的,我找到了解决方案。只需设置itemContainerStyle即可。 Holp它对其他人有帮助。

<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}}。当然,水平对齐也类似。