如果 Grid.Row 已知,如何获取网格行的子级

Gay*_*ale 5 xamarin.forms

我正在开发 xamarin.forms 应用程序,但我有无法解决的问题。

1)我有多个Grid相继包含按钮、标签和图像的s。现在,单击按钮我想更改图像或将其旋转到向下。但不知何故,我无法做到这一点。

我的第一次尝试是获取按钮的父级并使用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";
}
Run Code Online (Sandbox Code Playgroud)

upimagenull

非常感谢。

Ste*_*oix 6

我只是回答你的第一个问题

Grid.Row你说得对,你可以使用获取附加的可绑定属性的值public static int GetRow(BindableObject bindable)。现在过滤网格的子项非常容易。

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);
Run Code Online (Sandbox Code Playgroud)