Reputation: 2517
The code below should give you a pretty good idea of what I'm working with. Each Widget
gets displayed in the ListView
. But some of the widgets are "On Fire" and I would like to display their StreetName
in red. The problem is the record of which widgets are on fire is kept in the 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>
In a perfect world it would look something like:
<!- 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)}/>
Also, I don't think using the selected item will get the job done. The display of on fire widgets should be independent from the display of the selected item.
Upvotes: 1
Views: 706
Reputation: 637
we can tackle the problem about OnFire widgets having a separate color later.
You could define datatemplates and use a data template selector. Please see https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/selector
Each item that the list view is going to display will invoke the selector. if you can determine whether a widget is onFire by looking at some properties in the object. this method would work. if not let me know there are some other thoughts.
sorry I tried to format the output of this post.
Upvotes: 1