gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:17:27 25 4
gpt4 key购买 nike

我有一个 ListView,它从 ObservableCollection 绑定(bind)它的项目,还有一个 Button,它改变一个特定对象的“Amount”属性那个ObservableCollection。我想更改这些“金额”已更改的 ItemsBackgroundColor

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

最佳答案

一种方法是添加一个新属性,例如 HasAmountChanged,将视单元的背景颜色绑定(bind)到该属性,并使用 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 中的堆栈布局,这是设置背景颜色的地方:

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

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

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 值并处理它。

关于c# - 更改 Xamarin Forms 上 ListView 项的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51956076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com