[英]WPF: rows and columns in an ItemsControl
我試圖把既有的兒童ListView
和ItemsControl
行和列,通過設置與網格RowDefinitions
和ColumnDefinitions
作為ItemsPanel
屬性。
但是,當我將子控件放到第1行和第1列時,
<ItemsControl>
<ItemsControl.ItemsPanel>
<!-- Grid with rows & columns ... -->
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Grid.Row="4" Grid.Column="2" ... />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我該如何進行這項工作? 謝謝。
您應該設置包裝了ItemTemplate
根元素的容器的Grid.Row
和Grid.Column
附加屬性:
<ItemsControl x:Name="iccc">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>cell 2:2...</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Row" Value="1" />
<Setter Property="Grid.Column" Value="1" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
在“ ItemTemplate”而不是“ ItemPanel”中設置“ Grid”。 請參閱此處的示例: http : //www.wpf-tutorial.com/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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.