Xamarin 如何在一行4个和2个按钮之间切换?
我有以下代码:Xamarin 如何在一行4个和2个按钮之间切换?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有以下代码: <Grid Grid.Row="2" x:Name="buttonGrid" Padding="10,0,10,0" HorizontalOptions="FillAndExpand" VerticalOptions="Center"> <Button x:Name="aButton" Style="{StaticResource pointButton}" Text="Don't Know" />
<Grid Grid.Row="2" x:Name="buttonGrid" Padding="10,0,10,0" HorizontalOptions="FillAndExpand" VerticalOptions="Center">
<Button x:Name="aButton" Style="{StaticResource pointButton}" Text="Don't Know" />
<Button x:Name="bButton" Style="{StaticResource pointButton}" Text="Very Hard" />
<Button x:Name="cButton" Style="{StaticResource pointButton}" Text="Hard" />
<Button x:Name="dButton" Style="{StaticResource pointButton}" Text="Easy" />
<Button x:Name="nButton" Style="{StaticResource pointButton}" Text="Don't Know" />
<Button x:Name="yButton" Style="{StaticResource pointButton}" Text="Easy" />
</Grid>
我想让前四个或最后两个按钮出现在屏幕底部的一行
是否可以有多个Grid.Row=“2”并切换到后端C#中显示IsVisible的一个或另一个网格?我想这可以满足我的需要,但不确定这是否是一个好方法
我希望您能给我一些建议,因为现在我只是为每个按钮设置IsVisible,它不会像预期的那样在一行中显示它们。您肯定可以将两个或多个网格/容器分配给给定的行。使用isVisible显示/隐藏包含按钮组的容器应该可以正常工作 你想要的是这样的东西
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="9*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand" Grid.Row="1" IsVisible="{Binding FirstButtons}">
<Button x:Name="button1" Text="button1" Command="{Binding ChangeButtonsCommand}"/>
<Button x:Name="button2" Text="button2" Command="{Binding ChangeButtonsCommand}"/>
<Button x:Name="button3" Text="button3" Command="{Binding ChangeButtonsCommand}"/>
<Button x:Name="button4" Text="button4" Command="{Binding ChangeButtonsCommand}"/>
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand" Grid.Row="1" IsVisible="{Binding SecondButtons}">
<Button x:Name="buttonA" Text="buttonA" Command="{Binding ChangeButtonsCommand}"/>
<Button x:Name="buttonB" Text="buttonB" Command="{Binding ChangeButtonsCommand}"/>
</StackLayout>
</Grid>
ViewModelBase在这里
这将为您提供一行4个按钮,当您按下其中任何一个按钮时,它将仅切换到2个按钮。实现这一目标有很多方法,但我认为这正是你所追求的。请注意,Grid.Row属性是在网格内部的子元素中设置的,而不是在网格本身上设置的
public class MainViewModel : ViewModelBase
{
public ICommand ChangeButtonsCommand { get; private set; }
private bool firstButtons = true;
public bool FirstButtons
{
get { return firstButtons; }
set
{
firstButtons = value;
OnPropertyChanged();
}
}
private bool secondButtons = false;
public bool SecondButtons
{
get { return secondButtons; }
set
{
secondButtons = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
ChangeButtonsCommand = new Command(() =>
{
if (FirstButtons)
{
FirstButtons = false;
SecondButtons = true;
}
else
{
FirstButtons = true;
SecondButtons = false;
}
});
}