【发布时间】:2020-01-21 00:16:22
【问题描述】:
我有问题。我正在尝试将我的标签绑定到我的字典的值,所以我会在字典中获得一个标签 foreach 值。现在这是我的代码:
<ScrollView x:Name="categoryScrollView" HeightRequest="40" Orientation="Horizontal"
VerticalScrollBarVisibility="Never" HorizontalScrollBarVisibility="Never" HorizontalOptions="FillAndExpand">
<Frame CornerRadius="20" BackgroundColor="Black" BorderColor="DarkGray" HeightRequest="40">
<Label Text="{Binding categoryCollection[Value]}" FontSize="18" HorizontalTextAlignment="Center"
VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" TextColor="White" x:Name="txtCategory" />
</Frame>
</ScrollView>
如您所见,categoryCollection 是我的字典。
这是视图模型:
private Dictionary<int, string> _categoryCollection;
public Dictionary<int, string> categoryCollection
{
get
{
return _categoryCollection;
}
set
{
if (_categoryCollection != value)
{
_categoryCollection = value;
OnPropertyChanged();
}
}
}
但运行应用程序后,没有显示任何文字!?
我做错了什么?
【问题讨论】:
-
那行不通。您只能将集合绑定到 IEnumerable,而 Dictionary 不是 IEnumerable,您可以使用
categoryCollection.SelectMany (d => d.Value).ToList();将值提取到 List
标签: c# xamarin xamarin.forms xamarin.android xamarin.ios