这里有三个网格,点击Grid3控件后,再看看如何得到Grid1和Grid2的控件值,Xamarin

wmtdaxz3  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在使用网格,并试图使控制动态循环。
有3个网格,在单击Grid3控件后,我想获得Grid1和Grid2的控件值。
请参见图片。
任何帮助都是感激不尽的。谢谢!

代码隐藏:

Dictionary<string, string[]> myDictionary3 = _Model.getRoomStatusData(_Model.RoomTypesSel, _Model.StartTimeNames);
//Grid 1 x:Name="timelist"
for (int i = 0; i < _Model.StartTimeNames.Length; i++)
{   
    Label time = new Label() { Text = _Model.StartTimeNames[i], WidthRequest = 80 };               
    timelist.Children.Add(time, i, 0);               
}

//Grid 2 x:Name="roomtype"
for (int i = 0; i < roomtypes.Length; i++)
{
    roomtype.RowDefinitions.Add(new RowDefinition());           
    Label room = new Label() { Text = _Model.RoomTypesSel[i].Name, WidthRequest = 80 };      
    roomtype.Children.Add(room, 0, i);
}

List<string[]> roomstatu = new List<string[]>()
{
    new string[] {"1","2","3","4","5","6"}, new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"}
};
//Grid 3 x:Name="status"
int index = 0;
foreach (var item in roomstatu)
{
    status.RowDefinitions.Add(new RowDefinition());
    for (int i = 0; i < item.Length; i++)
    {
        status.ColumnDefinitions.Add(new ColumnDefinition());
        Label statuslabel = new Label() { Text = item[i], WidthRequest = 80};                           
        status.Children.Add(statuslabel, i, index);
    }
    index++;              
}
ar7v8xwq

ar7v8xwq1#

首先可以得到用户在状态网格中点击的标签的索引,然后通过索引可以得到标签的行位置和列位置。
我已经做了一个例子,它工作得很好,你可以试试下面的代码:

int index = 0;
        foreach (var item in roomstatu)
        {
            status.RowDefinitions.Add(new RowDefinition());
            for (int i = 0; i < item.Length; i++)
            {
                status.ColumnDefinitions.Add(new ColumnDefinition());
                Label statuslabel = new Label() { Text = item[i], WidthRequest = 80 };
                status.Children.Add(statuslabel, i, index);
                statuslabel.GestureRecognizers.Add((new TapGestureRecognizer
                {
                    Command = new Command(async (obj) =>
                    {
                        Label temp = obj as Label;
                        Grid gridtemp = temp.Parent as Grid;
                        int position = gridtemp.Children.IndexOf(temp);
                        int columnindex = position % timelist.Children.Count;
                        int rowindex = (position / roomtype.Children.Count) +1;
                        
                        string timevalue = ((Label)timelist.Children[columnindex]).Text;
                        string roomtypealue = ((Label)roomtype.Children[rowindex]).Text;
                        temp.Text = timevalue + "+"+ roomtypealue;
                    }),
                    CommandParameter = statuslabel,
                }));
            }
            index++;
        }

相关问题