1
votes

Is there a build in way to disable / gray out a ViewCell in a ListView? I have been looking trough the documentation but I couldn't find anything. Here is what I have so far.

 <ListView x:Name="lvNotes" ItemSelected="OnSelection">
          <ListView.ItemTemplate>
            <DataTemplate>
              <TextCell Text="{Binding Object.Name}" Detail="{Binding Object.Subject}"/>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
2
I do not believe there is a straight forward disable item at row X. Have you considered handling whether or not an item is disabled at the bound collection level or bound object level. IE I have an ObservableCollection<Car> set as the list source. Car has a bool "InStock". If the car is not instock I do not want the user to be taken to the details/purchase screen. When the user clicks the row I check the Cars instock property and determine my next step.ClintL
@ClintLandry Alright, so how to do the background color then?Trevi Awater
Check to see if listview gives a propertybinding for the viewcells background color and bind it to a field 'color' in car, have color set in the mutator for instock so that it is set to disabled color or enable color based on what instock is.ClintL

2 Answers

4
votes

For changing background you can easy use Triggers.

Here is your example of listView, but with the triggers. Background of the grid will turn gray when property Object.IsActive is set to false.

    <ListView x:Name="lvNotes" ItemSelected="OnSelection">
      <ListView.ItemTemplate>
        <DataTemplate>
            <Grid BackgroundColor="Green">
                <TextCell Text="{Binding Object.Name}" Detail="{Binding Object.Subject}"/>
                <Grid.Triggers>
                    <DataTrigger TargetType="Grid" Binding="{Binding Object.IsActive}" Value="False">
                        <Setter Property="BackgroundColor" Value="Gray"/>
                    </DataTrigger>
                </Grid.Triggers>
            </Grid>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
1
votes

I think you will a DataTemplateSelector for Xamarin.Forms to achieve this behaviour. From this blog post:

Supposed you want to put a list of items into a ListView but use a different DataTemplate for some of the items? In Microsoft XAML, you could set the ItemTemplateSelector property to a custom DataTemplateSelector and you’d be all set

Hope it helps you.