简体   繁体   English

如何在 Xaml 中更改 ListView 项目的背景颜色?

[英]How can I change background color of items of a ListView in Xaml?

I have a ListView , and I need to replace it's native colors (both of the selected item and other items) to different colors.我有一个ListView ,我需要将它的本机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.我可以将背景颜色更改为不同的颜色(请参阅下面的代码),但我不知道如何使其表现得像一个常见的ListView ,在选择时更改项目颜色。

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>

This is possible using Triggers as well in xaml but this is also going to work.这也可以在 xaml 中使用Triggers ,但这也将起作用。

To change color of the selected ViewCell , there is a simple work around without using custom renderer.要更改所选ViewCellcolor ,有一个简单的解决方法,无需使用自定义渲染器。 Make the Tapped event of your ViewCell as below使您的ViewCell Tapped如下

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

In your ContentPage 's cs file, implement the eventContentPage的 cs 文件中,实现事件

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;在你的ContentPage顶部声明lastCell就像这个ViewCell lastCell;

Output screenshot:输出截图:

在此处输入图像描述

You can't do something like that in XAML.你不能在 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.您可以执行类似建议的解决方案,如果它不起作用,您可以在 ViewCell 内放置一个 Grid 并更改其颜色,它是最接近 XAML 的唯一方法。 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.在 Android 上这相当简单,您可以编辑 styles.xml 来实现。 On other platforms you will need to write custom renderers.在其他平台上,您将需要编写自定义渲染器。

I have a ListView , and I need to replace it's native colors (both of the selected item and other items) to different colors.我有一个ListView ,我需要将其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.我可以将背景颜色更改为其他颜色(请参见下面的代码),但是我不知道如何使其表现为普通的ListView ,从而在选择时更改项目颜色。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM