【问题标题】:How to get the stacklayout that contains a button when the button is clicked?单击按钮时如何获取包含按钮的stacklayout?
【发布时间】:2021-04-21 21:29:54
【问题描述】:

我正在使用包含其他一些元素和按钮的堆栈布局。

<StackLayout x:Name="layout">
   <Button Text="Button" Clicked="Action"/>
<StackLayout/>

有没有办法在点击时获取包含按钮的 StackLayout?

【问题讨论】:

  • 你想完成什么?
  • 我有一个包含一些其他堆栈的数组,我想在单击按钮时获取堆栈布局以将其删除

标签: c# xaml xamarin xamarin.forms stacklayout


【解决方案1】:

使用父属性

void Action(object sender, EventArgs args)
{
     var button = sender as Button;
     var stackLayout = button.Parent as StackLayout;
     stackLayout.Children.Remove(button); 
}

【讨论】:

  • 或者你可以使用layout.Remove(button),如果在你的xaml.cs中编码
  • 是的,前提是使用此事件处理程序的所有按钮都是布局的子级,但可能是 OP 对多个按钮使用相同的事件处理程序(即使是与布局不同的另一个 StackLayout 的子级) )。来自the op comment"我有一个包含其他堆栈的数组"
猜你喜欢
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
相关资源
最近更新 更多