本文介绍了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中的行和列
发布评论