C# 嵌套网格中的XAML数据绑定
我已经在XAML中创建了一个嵌套网格,目前看起来如下:C# 嵌套网格中的XAML数据绑定,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我已经在XAML中创建了一个嵌套网格,目前看起来如下: <Grid x:Name="mainGrid" > <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDe
<Grid x:Name="mainGrid" >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Grid.Row="{Binding Row}" Grid.Column="{Binding Col}">
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}" VerticalAlignment="Bottom" HorizontalAlignment="Center" Grid.Row="0" FontWeight="Bold" TextDecorations="Underline"/>
<ListView ItemsSource="{Binding Names}" Grid.Row="1"/>
</Grid>
</Grid>
以及类别定义:
class Test
{
public string Title { get; set; }
public int Row { get; set; }
public int Col { get; set; }
public ObservableCollection<string> Names
{
get
{
return new ObservableCollection<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
}
}
}
类测试
{
公共字符串标题{get;set;}
公共int行{get;set;}
公共整数列{get;set;}
公共可观测集合名称
{
得到
{
返回新的ObservableCollection(){“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”、“I”、“J”};
}
}
}
我在这里的想法是,对于外部网格定义(0,0&0,1&1,0&1,1)的每个部分,我会有4个具有行/列定义的对象。内部网格
将充当一个模板,就像列表视图中的项目模板
。但是,仅填充顶部(0,0)网格
我应该采取哪些步骤,以使内部网格
的行为类似于模板,并创建和填充所有4个网格?使用项控件
,其中项资源
是测试
的列表,并用外部网格填充ItemsPanel
,用内部网格填充ItemTemplate
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Grid.Row" Value="{Binding Row}"/>
<Setter Property="Grid.Column" Value="{Binding Col}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}" VerticalAlignment="Bottom" HorizontalAlignment="Center" Grid.Row="0" FontWeight="Bold" TextDecorations="Underline"/>
<ListView ItemsSource="{Binding Names}" Grid.Row="1"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
由于ItemsControl
将在ContentPresenter
中包装项目,您需要将Grid.Column
和Grid.Row
绑定到itemscontainerstyle
编辑
ItemsControl
是显示内容列表的最基本控件。控件,如ListBox
,ListView
,ComboBox
,DataGrid
,甚至MenuItem
,它们都继承自ItemsControl
在顶部添加更多功能,如选择或列视图
ItemsPanel
定义常规项目容器的外观,ItemTemplate
定义集合中每个项目的外观
每个ItemsControl
都有其项目容器类型。对于ListBox
它将是ListBoxItem
,对于ListView
它将是ListViewItem
,依此类推,对于ItemsControl
它是ContentPresenter
。它只是一个可视化容器,列表中的每一项都作为一个内容,然后根据层次结构转换为可视化。在可视化树中,此容器是“项目”面板的直接子级,因此必须对其设置“项目列表”Grid.Column
和/或Grid.Row
。另外,例如,对于ListBox
,保存IsSelected
属性的是ListBoxItem
。更改容器或获取容器的数据的方法是通过<代码> ItEnguleStult。您可以考虑实际使用ItEnSt控件,基本上是当前的内部网格作为项目模板,当前的外部网格作为ItMe容器模板,并通过ItMeStand给您的集合提供数据。Yea,像这样…+例如,谢谢你。我以前没有听说过ItemsControl控件,所以我会做一些研究来进一步了解它。ItemsControl
是显示列表的最基本控件。控件,如ListBox
,ListView
,ComboBox
,DataGrid
,甚至MenuItem
,它们都继承自ItemsControl
在顶部添加更多的功能,如选择或列视图。因此,为了澄清,ItemsControl
承载一个集合,ItemsPanel
定义常规容器的外观,ItemTemplate
定义集合中每个项目的外观?您能解释一下为什么行和列需要位于此ItemsContainerStyle
中吗?谢谢。
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Grid.Row" Value="{Binding Row}"/>
<Setter Property="Grid.Column" Value="{Binding Col}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}" VerticalAlignment="Bottom" HorizontalAlignment="Center" Grid.Row="0" FontWeight="Bold" TextDecorations="Underline"/>
<ListView ItemsSource="{Binding Names}" Grid.Row="1"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>