【发布时间】:2019-08-08 18:13:12
【问题描述】:
我有一个ListView,如下所示。
如何将字典绑定到 ListView Itemsource 以便我的标签作为键和条目具有价值?
我不知道该怎么做
我试过了,但我得到 空引用异常
<ListView x:Name="ItemsListView" VerticalOptions="FillAndExpand" SeparatorVisibility="None" HasUnevenRows="true" ItemsSource="{Binding dictionary}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Text="{Binding Key}" Grid.Row="1" Grid.Column="0" Style="{DynamicResource lblTitle}" />
<Entry x:Name="test" Text="{Binding Value}" Grid.Row="1" Grid.Column="1" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
查看模型
public List<string> Key
{
get { return key; }
set
{
SetProperty(ref key, value);
}
}
public List<Int32> Value
{
get { return val; }
set
{
SetProperty(ref val, value);
}
}**
for (int i = 0; i < AllProductsList.Count; i++)
{
Value.Add(0);
//Value = new ObservableCollection<Int32>(val);
}
for (int j = 0; j < AllProductsList.Count; j++)
{
for (int k = 0; k < Value.Count; k++)
{
if (j == k)
{
dictionary[Key[j]] = Value[k];
}
}
【问题讨论】:
-
预期结果是例如。 item1 10 item2 20 item3 30 item4 40 列表数会根据需求动态变化
-
如果您遇到 NullReferenceException,请包含相应的堆栈跟踪,以便我们帮助您缩小代码失败的范围
-
你在哪里初始化
dictionary?这些for循环在哪里? -
什么类型的源数据,你能展示一下吗?也许你可以将源数据转换为 ViewModel 可以直接在 ListView 中使用。
标签: forms xamarin nullreferenceexception