繁体   English   中英

更改 Xamarin Forms 上 ListView 项的背景颜色

[英]Change the Background Color of an ListView Item on Xamarin Forms

我有一个ListView ,它从ObservableCollection绑定它的项目,还有一个Button改变ObservableCollection的特定对象的“Amount”属性。 我想更改“数量”已更改的这些ItemsBackgroundColor颜色。

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

一种方法是添加一个新属性,例如 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();
    }
}

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

您可以实现一组布尔值,并在 Amount 更改时将它们更改为 true。 然后您可能想要为每个 ListView 的颜色创建一个自定义渲染器。

暂无
暂无

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

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