WPF:ItemsControl中的行和列

编程入门 行业动态 更新时间:2024-10-28 13:27:57
本文介绍了WPF:ItemsControl中的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图通过将RowDefinitions和ColumnDefinitions设置为ItemsPanel属性的网格来将ListView和ItemsControl的子级放在行和列中.

I've tried putting children of both a ListView and an ItemsControl in rows and columns, by setting a grid with RowDefinitions and ColumnDefinitions as the ItemsPanel property.

但是当我放置时,子控件始终与第1行和第1列对齐

However the child control always aligns to Row 1 and Column 1, when I put

<ItemsControl> <ItemsControl.ItemsPanel> <!-- Grid with rows & columns ... --> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Grid.Row="4" Grid.Column="2" ... /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>

我该如何进行这项工作?谢谢.

How can I make this work? Thank you.

推荐答案

在"ItemTemplate"(而不是"ItemPanel")中设置"Grid".在此处查看示例: www.wpf-tutorial/list-controls /itemscontrol/

Set the ´Grid´ in the ´ItemTemplate´ rather than the ´ItemPanel´. See example here: www.wpf-tutorial/list-controls/itemscontrol/

public class Person { public string FirstName { get; set; } public string LastName { get; set; } } public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); PersonCollection = new ObservableCollection<Person>() { new Person() { FirstName = "John", LastName = "Doe" }, new Person() { FirstName = "Richard", LastName = "Bryson" }, new Person() { FirstName = "Bill", LastName = "Gates" }, new Person() { FirstName = "Adam", LastName = "Sandler" } }; itemsControl.ItemsSource = PersonCollection; } public ObservableCollection<Person> PersonCollection { get; set; } }

<ItemsControl x:Name="itemsControl"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid Margin="0,0,0,5"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="{Binding Path=FirstName}" /> <TextBlock Grid.Column="1" Text="{Binding Path=LastName}" /> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>

更多推荐

WPF:ItemsControl中的行和列

本文发布于:2023-11-12 03:04:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WPF   ItemsControl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!