【问题标题】:Get layout from button从按钮获取布局
【发布时间】:2018-07-14 22:17:51
【问题描述】:

在 Xamarin 表单项目中,我试图从按钮单击事件中获取 StackLayout。当单击一个按钮时,StackLayout 可以有 1-3 个按钮

static void PollButtonClick(object sender, EventArgs e)
    {
        Button button = sender as Button;
        button.IsEnabled = false;
    }

但我需要能够禁用所有按钮。我尝试寻找类似 button.GetLayout 的东西,这样我就可以遍历 Children 以找到所有按钮,但没有找到任何方法。

【问题讨论】:

    标签: c# xamarin xamarin.forms


    【解决方案1】:

    您可以使用ElementParent 属性,然后循环遍历该ElementChildren,如果需要,跳过作为发件人的Button

    例子:

    void Handle_Clicked(object sender, System.EventArgs e)
    {
        foreach (var child in ((sender as Button).Parent as StackLayout).Children)
        {
            if (child is Button && !child.Equals(sender))
            {
                child.IsEnabled = false;
            }
        }
    }
    

    【讨论】:

    • 谢谢!完美运行!
    • @Daniel 没问题,很高兴为您提供帮助?
    猜你喜欢
    • 2019-06-05
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多