问题
I have created a gridview by using list view.
There is 2 level of nodes and I would like to indent the child node.
Therefore, I have set the margin in the <ItemPresenter>
.
The result looks pretty good but the second and the third column also been indent and not align according to its header column.
Any idea to indent only the first column but not the rest column?
xaml
<ListView Name="listViewResult" Margin="10,231,0,-299" BorderBrush="#FF000000" BorderThickness="1" >
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="280" DisplayMemberBinding="{Binding GrandChildNodeData}" />
<GridViewColumn Header="Date/ Time" Width="160" DisplayMemberBinding="{Binding Time}" />
<GridViewColumn Header="State" Width="160" DisplayMemberBinding="{Binding State}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontWeight="Bold" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" Foreground="Silver" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
<TextBlock Text=" item(s)" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
<ItemsPresenter Margin="20,0,0,0" />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
result

回答1:
This question has been answered by Magnus in this forum
"You are setting the Margin of the entire GroupItem. You should define a CellTemplate for the first column and set the Margins in there:"
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Margin="20 0 0 0"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Date/ Time" Width="160" DisplayMemberBinding="{Binding Time}" />
<GridViewColumn Header="State" Width="160" DisplayMemberBinding="{Binding State}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontWeight="Bold" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" Foreground="Silver" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
<TextBlock Text=" item(s)" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
来源:https://stackoverflow.com/questions/26247581/indent-first-column-in-gridview-of-listview