我是WPF的新手。我试图制作Listview
表。我想拆分一些列。例如:
parent column1 | parent column2
column1.1 | column 1.2 | column2.1 | column 2.2 |column 2.3
我在这里找到了类似的问题:WPF: Spilt GridView header into two rows where the bottom row is seperated into two colums 但那里的答案并不适合我。我无法发表评论并要求进一步解释,因为我没有足够的声誉来发表评论。当我做同样的事情时,在我的情况下,列显示没问题,但它们作为一列。子列不起作用。 这是我的代码示例:
<ListView Margin="20" Name="purchaseSummaryList" ItemsSource="{Binding purchaseSummaryCol, Mode=Default}">
<ListView.View>
<GridView>
<GridView.Columns>
//single example that works fine
<GridViewColumn Header="ID" Width="100" DisplayMemberBinding="{Binding id, Mode=Default}" /> //single example that works
//parent column that acts as one column
<GridViewColumn Width="300" DisplayMemberBinding="{Binding pay, Mode=Default}">
<GridViewColumnHeader>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid VerticalAlignment="Center" ShowGridLines="False" Width="300">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" HorizontalAlignment="Center" Text="PARENT"/>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" HorizontalAlignment="Center" Padding="5" Text="Child1" />
<TextBlock Grid.Column="1" HorizontalAlignment="Center" Padding="5" Text="Child2"/>
<TextBlock Grid.Column="2" HorizontalAlignment="Center" Padding="5" Text="Child3"/>
</Grid>
</Grid>
</Grid>
</GridViewColumnHeader>
</GridViewColumn>
<GridView.Columns>
<GridView>
<ListView.View>
<ListView>
我知道很明显,只有一列是父列,因为我没有将任何内容绑定到子列。但问题是,我做不到。它不允许我对孩子DisplayMemberBinding
使用TextBlock
。我也无法在GridViewColumn
内添加新的Grid
。我不知道如何对此进行建模。
有人可以帮我解决这个问题吗?我也很欣赏一个很好的WPF教程链接,我可以在哪里学习。谢谢。
答案 0 :(得分:2)
这可能是您正在寻找的吗?
ListViewItem parent1 = new ListViewItem("Parent1");
parent1.SubItems.Add("ChildItem");
parent1.SubItems.Add("ChildItem");
parent1.SubItems.Add("ChildItem");
ListViewItem parent2 = new ListViewItem("Parent2");
parent2.SubItems.Add("ChildItem");
parent2.SubItems.Add("ChildItem");
parent2.SubItems.Add("ChildItem");
ListViewItem parent3 = new ListViewItem("Parent3");
parent3.SubItems.Add("ChildItem");
parent3.SubItems.Add("ChildItem");
parent3.SubItems.Add("ChildItem");
ListViewXY.Items.AddRange(new ListViewItem[] {parent1 ,parent2 ,parent3 });
答案 1 :(得分:1)
它需要更多的工作,特别是关于我没有尝试过的值的绑定,但这适用于标题:
<ListView ItemsSource="{Binding PurchaseSummaryCol}" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="100" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="PARENT" Width="300" DisplayMemberBinding="{Binding Pay}" HeaderTemplate="{StaticResource MultiColumnHeaderTemplate}"
CellTemplate="{StaticResource MultiColumnCellTemplate}" />
</GridView>
</ListView.View>
</ListView>
然后,您需要拥有标题和单元格模板的资源:
<DataTemplate x:Key="MultiColumnHeaderTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
>RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding}" HorizontalAlignment="Center"/>
<GridViewHeaderRowPresenter HorizontalAlignment="Stretch" Grid.Row="1" Columns="{StaticResource SubColumnCollection}"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="MultiColumnCellTemplate">
<Grid>
<GridViewRowPresenter HorizontalAlignment="Stretch" Columns="{DynamicResource SubColumnCollection}"/>
</Grid>
</DataTemplate>
现在只剩下定义子列表集合了:
<GridViewColumnCollection x:Key="SubColumnCollection">
<GridViewColumn Header="Child1" Width="100" DisplayMemberBinding="{Binding Child1}"/>
<GridViewColumn Header="Child2" Width="100" DisplayMemberBinding="{Binding Child2}"/>
<GridViewColumn Header="Child3" Width="100" DisplayMemberBinding="{Binding Child3}"/>
</GridViewColumnCollection>
我没有调查,但孩子们的绑定没有显示,除了它有效。
PS:Microsoft建议使用CamelCasing作为属性。