下面的代码应该能让你很好地了解我在用什么。每个
Widget
显示在
ListView
StreetName
穿红色的。问题是哪些小部件着火的记录保存在
WidgetsViewModel
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SickProgram.Views.Pages.WidgetsPage"
xmlns:ViewModels="clr-namespace:SickProgram.ViewModels">
<ContentPage.BindingContext>
<ViewModels:WidgetsViewModel />
</ContentPage.BindingContext>
<StackLayout>
<ListView ItemsSource="{Binding Widgets}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding StreetName}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
在一个
完美
世界看起来像:
<!- OnFire is a method in the WidgetsViewModel that takes in a widget Id and returns a color. ->
<Label Text="{Binding StreetName}" Color={Binding BindingContext.Parent.OnFire(BindingContext.Id)}/>