我正在开发 xamarin.forms 应用程序,但我有无法解决的问题。
1) 我有多个 Grid
一个接一个,包含按钮、标签和图像。现在,单击按钮时,我想更改图像或将其向下旋转。但不知何故,我无法做到。
我的第一次尝试是获取按钮的父级并使用 FindByName
查找图像方法。但是FindByName
返回null,但调试时我可以在父级中看到网格。
我的第二次尝试是获取按钮行并找到该行中的所有控件。因为我的图像与按钮在同一行。
我的结构是这样的
我的网格工作正常,但只有旋转问题。
pubic void OnButtonClicked(object sender, EventArgs e)
{
var SenderButton = (Button)sender;
var row = Grid.GetRow(SenderButton); // Here i get row = 0 but dont know how to find other controls on same row.
Image upimage = SenderButton.Parent.FindByName<Image>("imageExpand"); // imageExpand is my image name in grid.
upimage.Source = "upimage.png";
}
这里
upimage
是 null
.非常感谢你。
最佳答案
I'm only replying to your first question
你没看错,你可以得到附加的可绑定(bind)属性的值
Grid.Row
使用 public static int GetRow(BindableObject bindable)
.现在过滤网格的 child 很容易。var button = (Button)sender;
var row = Grid.GetRow(button);
var grid = button.Parent as Grid;
//assuming the image is in column 1
var image = grid.Children.Where(c => Grid.GetRow(c) == row && Grid.GetColumn(c)==1);
关于xamarin.forms - 如果 Grid.Row 已知,如何获取网格行的子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39475959/