Indent first column in GridView of ListView

痞子三分冷 提交于 2019-12-13 04:23:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!