简体   繁体   English

当用户在Xamarin Forms上按下按钮时,显示按钮列表

[英]Display a list of Buttons when user presses a Button on Xamarin Forms

I have a Xamarin.Forms app that uses the following XAML. 我有一个使用以下XAML的Xamarin.Forms应用程序。

<StackLayout Grid.RowSpan="8" 
                     Grid.Column="0"
                     >
          <Label Text="{Binding ActiveMenu.ChildMenuItems.Count}" />
        <ListView x:Name="weapponType"
                  ItemsSource="{Binding ActiveMenu.ChildMenuItems}"
                  BackgroundColor="Blue"
                 >
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell >
                  <Button Text="{Binding Text}"
                          FontSize="14"
                          />
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>

        </ListView>
      </StackLayout>

It displays a list of Buttons as follows 它显示按钮列表,如下所示

---- EVENT
-----PROG
-----STAT
-----MENU1
-----MENU2

When the user clicks EVENT I want it to display a list of buttons 当用户单击“ EVENT我希望它显示按钮列表

---Button1
---Button2
---Button3

where 哪里

<Button x:Name="MenuBtn1" Text="Btn1" Command="{Binding Command1}"
<Button x:Name="MenuBtn2" Text="Btn2" Command="{Binding Command2}"
<Button x:Name="MenuBtn3" Text="Btn3" Command="{Binding Command3}"

It would be really great if someone can guide me on how to achieve this. 如果有人可以指导我如何实现这一目标,那将是非常不错的。

u need add the click event "OnBtClick" for each button as follow code: 您需要为每个按钮添加点击事件“ OnBtClick”,如下所示:

 <ListView ItemsSource="{x:Static local:ListViewMode.All}" RowHeight="100">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <ViewCell.View>
            <Button Text="{Binding BtName}" Clicked="OnBtClick"></Button>
          </ViewCell.View>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

At the code behind: 在后面的代码中:

 async void OnBtClick(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new ContentPage
            {
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        new Button {

                            Text = "button1"
                        },
                        new Button {

                           Text = "button2"
                        },
                        new Button {

                            Text = "button3"
                        }
                    }
                }
            });
        }

When you click the each button it will show the three button I defined above. 当您单击每个按钮时,它将显示我上面定义的三个按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM