【发布时间】:2020-02-24 04:59:29
【问题描述】:
我有一个只有 1 列的网格,我希望我的按钮适合网格的大小,我尝试使用 StackLayout 和 Frame,现在我尝试使用 Grid,也尝试使用 1 列和 1 行,但结果是一样的,我会附上一张照片来说明发生了什么:
我需要红色按钮来拉伸,以便它们填充设备的宽度,我尝试在水平选项中使用 StartAndExpand、Fill 和 FillAndExpand 属性,但它们不起作用。 使用 Fill 和 FillAndExpand 它可以工作,但按钮文本会居中,然后出现一个错误,每次我点击一行时,文本都会向左移动,并保持在那里,除非我向下滚动列表视图并再次返回顶部。
这是我的代码:
<ListView x:Name="GroupsList"
ItemsSource="{Binding Dishes}"
IsGroupingEnabled="True"
SeparatorColor="Black"
SeparatorVisibility="Default"
HasUnevenRows="True">
<ListView.Behaviors>
<behaviorsPack:SelectedItemBehavior Command="{Binding BindingContext.SelectedDishCommand, Source={x:Reference DishesPage}}"></behaviorsPack:SelectedItemBehavior>
</ListView.Behaviors>
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell Height="50">
<Grid VerticalOptions="FillAndExpand"
BackgroundColor="LightSlateGray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button BackgroundColor="DarkRed"
Grid.Column="0"
BorderColor="Transparent"
BorderWidth="0"
Text="{Binding Key}"
TextColor="White"
HorizontalOptions="StartAndExpand"
Command="{Binding BindingContext.SelectGroupHeader, Source={x:Reference DishesPage}}"
CommandParameter="{Binding Key}"
ImageSource="next_disclosure"
ContentLayout="Right"></Button>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView Padding="2, 5, 5, 0">
<Frame Padding="2"
HasShadow="False"
BackgroundColor="White">
<StackLayout Orientation="Horizontal">
<Label Margin="10"
Text="{Binding Name}"
TextColor="Black"
FontSize="Medium"
HorizontalOptions="StartAndExpand"></Label>
</StackLayout>
</Frame>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
希望有人能帮帮我。
【问题讨论】:
-
您尝试过Fill 或FillAndExpand 吗?如果你想要的只是一个按钮,你可能甚至不需要网格。
-
这是一个 ListView,因此 List 中的每个标题行都有自己独立布局的 Grid。您没有指定任何宽度或水平布局指南,因此每个 Grid 都足够大以适合其内容。
-
我尝试过填充、填充和扩展,并且使按钮适合所有内容,文本保持在中心,当我需要它在左侧时,当我单击按钮时,文本向左移动,然后在我滚动@BenReierson 时返回中心
-
我试过没有父布局,只有按钮,我也有同样的问题@Jason
-
按钮始终将其文本居中。您可以尝试使用标签和图像合成一些东西。您希望披露图标在每一行中都右对齐吗?
标签: xaml xamarin xamarin.forms