【发布时间】:2016-01-04 04:13:17
【问题描述】:
我在填充和显示 XLabs.Forms.Control:AutoCompleteView 的建议列表时遇到了困难。我已经将 ViewModel 中的 observable 集合绑定到 autocompleteview xaml 的 Suggestions 属性。
根据我的调试代码(即只是一个将查询返回的内容写入调试输出的循环),我的查询正在返回项目,所以我认为问题在于仅显示所述项目。
这是 Xaml 和 ViewModel 的代码(Store 类有一个 StoreName 属性/字段)
XAML
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="SugestionItemTemplate">
<ViewCell Height="60">
<ViewCell.View>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Label Text="{Binding StoreName}" VerticalOptions="Center" HorizontalOptions="Start" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout HorizontalOptions="Center" Spacing="10">
<StackLayout.BindingContext>
<vm:CreateSaleViewModel />
</StackLayout.BindingContext>
<Label Text="Store" />
<controls:AutoCompleteView Placeholder="Type a store"
SuggestionItemDataTemplate="{StaticResource SugestionItemTemplate}"
Text="{Binding StoreQuery}"
ShowSearchButton="True"
SearchBackgroundColor = "White"
SearchCommand ="{Binding SearchCmd}"
Suggestions="{Binding StoreSuggestions}" />
</StackLayout>
视图模型
class CreateSaleViewModel
{
// Query Variables
public string StoreQuery { get; set; }
// Query Suggestions
public ObservableCollection<Store> StoreSuggestions { get; private set; }
public ICommand SearchCmd { get; set; }
public CreateSaleViewModel()
{
SearchCmd = new Command(Search);
}
private async void Search()
{
StoreSuggestions = await App.AzureDataStore.SearchStoresAsync(StoreQuery);
}
}
【问题讨论】:
-
您找到解决方案了吗?我也在寻找解决方案。谢谢
-
我刚刚认识到这里缺少 Suggestions="{Binding StoreSuggestions}" /> Mode=Twoway。应该是 Suggestions="{Binding StoreSuggestions, Mode=TwoWay}"
-
还没试过迈克尔戴维斯的回答。我们的团队决定使用不同的技术来代替 xamarin.forms
标签: autocomplete xamarin xamarin.forms xlabs