作者:手机用户2502905627_315 | 来源:互联网 | 2023-01-19 11:45
我有一堂课,以替代项目的背景颜色,但是如果删除项目,则背景颜色不会更新。删除项目后是否可以刷新背景颜色?
备用颜色的代码。类列表视图:
public class AlternatingRowListView : ListView
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
var listViewItem = element as ListViewItem;
if (listViewItem != null)
{
var index = IndexFromContainer(element);
if (index % 2 == 0)
{
listViewItem.Background = new SolidColorBrush(Colors.LightBlue);
}
else
{
listViewItem.Background = new SolidColorBrush(Colors.Transparent);
}
}
}
}
代码xaml:
item 1
item 2
item 3
item 4
提前致谢。
1> Justin XL..:
您只需要AlternatingRowListView
稍微扩展一下已扩展的控件即可实现所需的功能。
您可以通过订阅的VectorChanged
事件来监视何时将某个项目从列表中删除Items
,然后您就可以(直观地)遍历已删除项目下面的所有已实现项目,并相应地更改其背景颜色。
这样的事情会做-
public AlternatingRowListView()
{
DefaultStyleKey = typeof(ListView);
Items.VectorChanged += OnItemsVectorChanged;
}
private void OnItemsVectorChanged(IObservableVector sender, IVectorChangedEventArgs args)
{
// When an item is removed from the list...
if (args.CollectiOnChange== CollectionChange.ItemRemoved)
{
var removedItemIndex = (int)args.Index;
// We don't really care about the items that are above the deleted one, so the starting index
// is the removed item's index.
for (var i = removedItemIndex; i