首页 > 解决方案 > 在 Xamarin 窗体上更改 ListView 项的背景颜色

问题描述

我有一个ListView将它的项目从一个绑定到一个ObservableCollection,一个Button改变一个特定对象的“数量”属性ObservableCollection。我想改变BackgroundColor那些Items“金额”已经改变的人。

我已经为此寻找解决方案,但找不到任何解决方案。
有人知道解决这个问题的方法吗?

标签: c#androidlistviewxamarinxamarin.forms

解决方案


一种方法是添加一个新属性,例如 HasAmountChanged,将视单元的背景颜色绑定到该属性,然后使用 ValueConverter 设置颜色。这将类似于以下内容:

具有以下属性的对象类:

public class MyObject : INotifyPropertyChanged
{
    double amount;
    bool hasAmountChanged = false;

    public event PropertyChangedEventHandler PropertyChanged;

    public MyObject(double amount)
    {
        this.amount = amount;
    }

    public double Amount
    {
        get => amount;
        set
        {
            if (amount != value)
            {
                amount = value;
                OnPropertyChanged(nameof(Amount));
                HasAmountChanged = true;
            }
        }
    }

    public bool HasAmountChanged
    {
        get => hasAmountChanged;
        set
        {
            if (hasAmountChanged != value)
            {
                hasAmountChanged = value;
                OnPropertyChanged(nameof(HasAmountChanged));
            }
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

风景。注意 ViewCell 内的 stacklayout,这是设置背景颜色的地方:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:Delete"
         x:Class="Delete.MainPage">

<ContentPage.Resources>
    <ResourceDictionary>
        <local:ListViewBackgroundColorConverter x:Key="ListViewColorConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

<StackLayout>
    <Button Text="Click Me" Clicked="ButtonClicked" />

    <ListView ItemsSource="{Binding MyItemsSource}" HasUnevenRows="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Spacing="15" 
                                 BackgroundColor="{Binding HasAmountChanged, Converter={StaticResource ListViewColorConverter}}" 
                                 HorizontalOptions="FillAndExpand" 
                                 VerticalOptions="FillAndExpand">
                        <Label Text="FOO 1"/>
                        <Label Text="{Binding Amount}"/>
                        <Label Text="{Binding HasAmountChanged}" />
                        <Label Text="FOO 4"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

</StackLayout>

为了完整起见,包含视图背后的代码:

public partial class MainPage : ContentPage
{
    public ObservableCollection<MyObject> MyItemsSource { get; set; }

    public MainPage()
    {
        InitializeComponent();

        MyItemsSource = new ObservableCollection<MyObject>
        {
            new MyObject(1.14),
            new MyObject(1.14),
            new MyObject(1.14),
            new MyObject(1.14),
            new MyObject(1.14),
        };

        BindingContext = this;
    }

    void ButtonClicked(object sender, EventArgs e)
    {
        var rnd = new Random();

        var myObject = MyItemsSource[rnd.Next(0, MyItemsSource.Count)];

        myObject.Amount = 5.09;
    }
}

最后是最重要的部分,转换器:

public class ListViewBackgroundColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (bool)value ? Color.LawnGreen : Color.DarkRed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

请注意,您实际上想要检查它是否是一个布尔值并处理它。


推荐阅读