admin管理员组

文章数量:1122846

I want to set width of items columns the same as the width of listview.headers. the width of headers isn't even as the width of headers. It must be the way to bind these two properties.

  <Listview>
   <ListView.Header>
     <Grid>
      <Grid.ColumnDefinitions Width ="1*">
      <Grid.ColumnDefinitions Width ="Auto">
       <Label 
        Text = "Title of a book"
        Grid.Column ="0" 
        HorizontalOptions= "StartAndExpand"/>
     <Label 
      Text = "Date"
      Grid.Column ="1" 
      HorizontalOptions= "Center"/>
     </Grid>
     </Listview.Heder>
     <Listview.Items>
      <ViewCell>
       <Grid>
      <Grid.ColumnDefinitions Width ="1*">
       <Grid.ColumnDefinitions Width ="Auto">
        <Label 
        Text = "{Binding Library}"
        Grid.Column ="0" 
        LineBreakMode=TailTrumcation
        HorizontalOptions= "StartAndExpand"/>
       <Label 
         Text = "{Binding RealiseDate}"
        Grid.Column ="1" 
        HorizontalOptions= "Center"/>
        </Grid>
       </ViewCell>
     </Listview.Items>
    </Listview>

The columns' width and headers' width aren't. How to bind headers width with items width?

I want to set width of items columns the same as the width of listview.headers. the width of headers isn't even as the width of headers. It must be the way to bind these two properties.

  <Listview>
   <ListView.Header>
     <Grid>
      <Grid.ColumnDefinitions Width ="1*">
      <Grid.ColumnDefinitions Width ="Auto">
       <Label 
        Text = "Title of a book"
        Grid.Column ="0" 
        HorizontalOptions= "StartAndExpand"/>
     <Label 
      Text = "Date"
      Grid.Column ="1" 
      HorizontalOptions= "Center"/>
     </Grid>
     </Listview.Heder>
     <Listview.Items>
      <ViewCell>
       <Grid>
      <Grid.ColumnDefinitions Width ="1*">
       <Grid.ColumnDefinitions Width ="Auto">
        <Label 
        Text = "{Binding Library}"
        Grid.Column ="0" 
        LineBreakMode=TailTrumcation
        HorizontalOptions= "StartAndExpand"/>
       <Label 
         Text = "{Binding RealiseDate}"
        Grid.Column ="1" 
        HorizontalOptions= "Center"/>
        </Grid>
       </ViewCell>
     </Listview.Items>
    </Listview>

The columns' width and headers' width aren't. How to bind headers width with items width?

Share Improve this question asked yesterday RadekRadek 11 bronze badge New contributor Radek is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

You can try this code.

<ContentPage.Resources>
    <ResourceDictionary>
        <ColumnDefinitionCollection x:Key="SharedColumnDefinitions">
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </ColumnDefinitionCollection>
    </ResourceDictionary>
</ContentPage.Resources>

<ListView>
    <ListView.Header>
        <Grid ColumnDefinitions="{StaticResource SharedColumnDefinitions}">
            <Label 
                Text="Title of a book"
                Grid.Column="0" 
                HorizontalOptions="StartAndExpand"/>
            <Label 
                Text="Date"
                Grid.Column="1" 
                HorizontalOptions="Center"/>
        </Grid>
    </ListView.Header>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid ColumnDefinitions="{StaticResource SharedColumnDefinitions}">
                    <Label 
                        Text="{Binding Library}"
                        Grid.Column="0" 
                        LineBreakMode="TailTruncation"
                        HorizontalOptions="StartAndExpand"/>
                    <Label 
                        Text="{Binding ReleaseDate}"
                        Grid.Column="1" 
                        HorizontalOptions="Center"/>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

本文标签: How to set width of grid column the same as the width of header in xamarin forms lsitviewStack Overflow