动态更改网格的子项

时间:2015-02-04 20:11:54

标签: c# .net mvvm xamarin xamarin.forms

我尝试使用Xamarin表单的网格视图来实现搜索建议功能。我想知道是否还有根据在搜索框中输入的文本动态更改网格的子项。

    grid.Children.Add(new Label
            {
                Text = x.Text,
                TextColor = Color.White,
                BackgroundColor = Color.Blue
            }, 0, x.Id);

2 个答案:

答案 0 :(得分:0)

如果要使用Grid,则应指定行和列,否则所有文本将相互重叠。您应该使用StackLayout而不是Grid。

hintSL  = new StackLayout(){
   HorizontalOptions= LayoutOptions.Fill,
   //Orientation = Vertical or Horizontal (however you want)
};

//if you want to add hintsSl into your grid
   Grid.SetRow(hintsl, 4);
   Grid.SetColumn(hintsl, 4);

searchText.Changed+=(s,e)=>{
   hintSL.Children.Clear();
   foreach(var hint in YourHintsList)
      hintsl.Children.Add(new Label(){Text=hint, and other properties});
};

答案 1 :(得分:0)

以下是两种方法: 首先是使用绑定。将标签的文本字段绑定到可根据用户条目更改的可绑定属性。

第二个是使标签成为任何持有网格的类的成员,而不是匿名。然后,您可以捕获用户输入文本时生成的事件(例如OnEntryChanged),并在代码中动态设置标签。