Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改ViewCell中的高度_C#_Xaml_Xamarin Forms - Fatal编程技术网

C# 如何更改ViewCell中的高度

C# 如何更改ViewCell中的高度,c#,xaml,xamarin-forms,C#,Xaml,Xamarin Forms,我正在尝试更改listview上的ViewCell,但下面的代码不适用于我: <DataTemplate> <ViewCell Height="100"> <StackLayout Orientation="Horizontal"> <Image Source="{Binding Seller.Thumbnail}}" Aspect="AspectFit" /> <Sta

我正在尝试更改listview上的ViewCell,但下面的代码不适用于我:

<DataTemplate>
    <ViewCell Height="100">
        <StackLayout Orientation="Horizontal">
            <Image Source="{Binding Seller.Thumbnail}}" Aspect="AspectFit" />
            <StackLayout Orientation="Vertical" >
                <Label Text="{Binding CouponName}" FontAttributes="Bold" FontSize="12" />
                <Label Text="{Binding EndOffer}" FontSize="11" />
            </StackLayout>
        </StackLayout>
    </ViewCell>
</DataTemplate>

高度/重量属性在Xamarin中大部分是只读的。您无法设置ViewCell的大小


但是,您可以更改StackLayout的HeightRequest属性以获得所需的内容。

设置ViewCell的高度应该可以

尝试将StackLayout的
垂直选项
水平选项
设置为
FillAndExpand

  • 如果所有单元格都设置了相同的大小,则 ListView本身
  • 如果要改为设置
    ViewCell.Height
    然后将
    ListView.hasRows
    设置为
    true
    (但它会对性能产生一些影响)

只要将网格的行定义高度设置为自动,无论标签包装在哪里。像这样:

<ListView ItemsSource="{Binding Gastos}" HasUnevenRows="True" SeparatorVisibility="None">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell >
                <Grid Padding="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Label Text="{Binding Id}" VerticalOptions="Start"/>
                    <Label Grid.Column="1" Text="{Binding Descripcion}" LineBreakMode="WordWrap"/>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>


仅当
ListView.HasUnderrows
TableView.HasUnderrows
属性设置为
true

时,为
视图单元格设置
高度将有效。2019年正确的做法是将此设置为固定高度:

<ListView RowHeight="100" />

如果不希望在所有行中固定高度,请使用:

<ListView HasUnevenRows="true" />


“一般”是这里的关键术语。然而,对于ViewCell,它是get/set。如果您查看VisualStudio中该属性上弹出的intellisense,这一点已得到确认。您必须记住,ListView本身的行高优先。因此,设置CellView的.Height似乎没有任何影响。它主要用于与ListVIew结合使用。hasRows=true
有关ListVIew.RowHeight与ViewCell.Height的讨论,请参见。重要提示:此答案适用于可能会换行到多行文本的标签。如果这样做,请不要忘记上面代码片段中的haslunterrows=“True”。True,但请参阅上面Daniel Luberda更完整的答案,上面已经说过。设置
haslunterrows=“True”
才是真正的答案