Xamarin中点击Grid3控件后如何获取Grid1和Grid2的控件值
动态Grid控件点击后获取关联Grid的控件值
我正在使用Grid控件,通过循环动态创建控件。现有3个Grid控件,点击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++; }
解决方案
核心思路是给Grid3的Label绑定点击事件,通过控件在Grid中的行列索引,关联获取Grid1和Grid2对应位置的控件值。
方法1:直接通过行列索引查找控件
修改Grid3的Label创建逻辑,添加点击事件并获取对应值:
//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}; // 添加点击手势识别器 var tapGesture = new TapGestureRecognizer(); tapGesture.Tapped += (sender, e) => { var clickedLabel = sender as Label; if (clickedLabel == null) return; // 获取当前Label在Grid3中的行列位置 int colIndex = Grid.GetColumn(clickedLabel); int rowIndex = Grid.GetRow(clickedLabel); // 获取Grid1(timelist)对应列的时间值 var timeLabel = timelist.Children.FirstOrDefault(c => Grid.GetColumn(c) == colIndex && Grid.GetRow(c) == 0) as Label; string timeValue = timeLabel?.Text ?? string.Empty; // 获取Grid2(roomtype)对应行的房型值 var roomLabel = roomtype.Children.FirstOrDefault(c => Grid.GetRow(c) == rowIndex && Grid.GetColumn(c) == 0) as Label; string roomTypeValue = roomLabel?.Text ?? string.Empty; // 此处可对获取到的timeValue和roomTypeValue进行业务处理 }; statuslabel.GestureRecognizers.Add(tapGesture); status.Children.Add(statuslabel, i, index); } index++; }
方法2:提前存储控件列表优化查找效率
如果控件数量较多,遍历Children集合会有性能损耗,可提前将Grid1和Grid2的控件存入列表,后续直接通过索引访问:
- 创建Grid1时存储控件列表:
List<Label> timeLabelList = new List<Label>(); //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); timeLabelList.Add(time); // 存入列表 }
- 创建Grid2时存储控件列表:
List<Label> roomLabelList = new List<Label>(); //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); roomLabelList.Add(room); // 存入列表 }
- Grid3的点击事件中直接通过索引获取值:
//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}; var tapGesture = new TapGestureRecognizer(); tapGesture.Tapped += (sender, e) => { var clickedLabel = sender as Label; if (clickedLabel == null) return; int colIndex = Grid.GetColumn(clickedLabel); int rowIndex = Grid.GetRow(clickedLabel); // 直接通过索引获取值,无需遍历 string timeValue = colIndex < timeLabelList.Count ? timeLabelList[colIndex].Text : string.Empty; string roomTypeValue = rowIndex < roomLabelList.Count ? roomLabelList[rowIndex].Text : string.Empty; // 业务处理逻辑 }; statuslabel.GestureRecognizers.Add(tapGesture); status.Children.Add(statuslabel, i, index); } index++; }
内容的提问来源于stack exchange,提问作者Raj
相关产品推荐
相关产品推荐

