Search code examples
listviewxamarinxamarin.formsdisabled-control

Xamarin.Forms disable item in ListView


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>

Solution

  • 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>