How can I change background color of a ListView in Xaml?

wpf listview change row color programmatically
how to change background color of selected item in listview wpf
wpf c# listview change background color
wpf listview alternate row color
xamarin forms viewcell background color
wpf listview style
wpf listview conditional formatting
dynamic listview in wpf

I have a ListView, and I need to replace its native colors (both of the selected item and other items) to different colors. I was not able to find how to do that. I can change the background color to something different (see my code below), but I don't know how to make it behave as a common ListView, changing item colors on selection.

Here is my code:

    <ListView x:Name="MenuItemsListView"
          SeparatorVisibility="None"
          HasUnevenRows="true"
          ItemsSource="{Binding MenuItems}">
  <ListView.Header>
    <Grid BackgroundColor="Black">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="10"/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="10"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="10"/>
      </Grid.RowDefinitions>
        <Image Grid.Column="1" Grid.Row="1" WidthRequest="50" HeightRequest="50" HorizontalOptions="StartAndExpand" Source="Assets\logo.png" />
    </Grid>
  </ListView.Header>
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell Height="100">
         <StackLayout Padding="15,10" 
                                 Orientation="Horizontal" 
                                 HorizontalOptions="FillAndExpand" 
                                 BackgroundColor="{StaticResource LogoBackgroundColor}">
              <Image WidthRequest="50" Source="{Binding IconSource}" />
              <Label VerticalOptions="FillAndExpand" 
                VerticalTextAlignment="Center" 
                Text="{Binding Title}" 
                FontSize="24"/>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

It might be possible using Triggers in xaml, who knows. But this as well serve your purpose. To change color of selected ViewCell, there is a simple work around without using custom renderer. Make Tapped event of your ViewCell as below

<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell Tapped="ViewCell_Tapped">            
       <StackLayout></StackLayout>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>

In your ContentPages cs file, implement the event

private void ViewCell_Tapped(object sender, System.EventArgs e)
{
    if(lastCell!=null)
        lastCell.View.BackgroundColor = Color.Transparent;
    var viewCell = (ViewCell)sender;
    if (viewCell.View != null)
    {
        viewCell.View.BackgroundColor = Color.Red;
        lastCell = viewCell;
    }
}

Declare lastCell at the top of your ContentPage like this ViewCell lastCell;

Output screenshot:

How do I set the background color of a listview item in WPF using , Try the Style property. One can add this code to the ListView and then it should set the background of the ListViewItems to the Brush color. I have a class that populates a ListView by passing a list of objects. The class uses reflection to see the properties of each object in order to generate the ListView. How could I change the background color of a row in the ListView. This page does exactly what I am looking for. The only problem is that my ListView is bound to the list of objects.

You can't do something like that in XAML. You may do something like proposed solution, if it is not working you may place a Grid inside ViewCell and change its color, it is the closest you can be to XAML only. But it is fairly inefficient.

The only true solution of your problem is on the native side. On Android it is fairly simple, you can edit styles.xml to achieve that. On other platforms you will need to write custom renderers.

Change background color of ListView row programmatically (wpf , When using the ItemContainerGenerator then be aware that the containers are generated asynchronously. The generator exposes a status changed event you  Change selection-color of WPF ListViewItem. you may have to set the background color of a single ListViewItem to something like green or red. c# wpf xaml listview.

You need to reset itemcontainerstyle. So that default selected items properties are overridden. Then you need to create some triggers for your DataTemplate item.

Below code is the style that resets your selecteditem default properties.

<Style x:Key="StyleListViewItem" TargetType="ListViewItem">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <ContentPresenter/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

wpf listview background color - MSDN, locked. wpf listview background color RRS feed If you didn't want to change it dynamically then that'd be more practical with a listview: I have a uwp App with a ListView. In the ListView i use a DataTemplate with the class of Tests. It displays the name of the Test and Points. What i want to accomplish is that a Trigger !? checks if the Points are greater than i.e.: 50 and then change the background color of the ListViewItem to red.

Different background colours for view cells in a ListView, I have a listview and I want to set two background colours for the cells in string Item { set; get; } public Color BgColor { set; get; } } > > Xaml: > I'm looking to set the background of a column in a WPF GridView. Many Google results have pointed towards setting the GridViewColumn.CellTemplate to change the appearance of a column. However, I'm met with an issue when setting the background color; it's not stretching to fill the cell: Here's the xaml I'm working with:

Change the forecolor of a ListViewItem in WPF, ItemTemplate> </ListView> <Button Click="Button_Click" Content="Go PaleGreen"/> Now changing the background color is easy. Just set the  You can't do something like that in XAML. You may do something like proposed solution, if it is not working you may place a Grid inside ViewCell and change its color, it is the closest you can be to XAML only. But it is fairly inefficient. The only true solution of your problem is on the native side.

[Solved] Updating Background Colors of a WPF ListView When the , I was surprised when, with no additional effort, the ListView cell contents were being updated updated to reflect the change in value. Is that  A simple Window with a ListView and a Button. In the XAML file, we set the name property colData to a GridViewColumn's header, which we can use in the code behind. In the Button's btnClick method, we programmatically create a style and assign it to the Grid View column header, and then we invoke the GridViewColumn's setvalue method the re

Comments
  • Do you need xaml only code behind is ok?
  • @CGPA6.4 I would prefer xaml, but I would consider code behind, in case I don't find anything for xaml. Thanks.
  • Strange, but the colors don't change, even though the execution does come to the line viewCell.View.BackgroundColor = Color.Red; ...
  • Is that not working for you? Same functionality I am using for my project.
  • Thank you. I am very new to Xamarin. BTW, I use UWP.
  • Thank you for your answer. I am getting this: The property 'TargetType' was not found in type 'ControlTemplate'.