简体   繁体   English

ListView将演员表项目绑定到ListViewItem

[英]ListView binding cast items to ListViewItem

I have a ListView bound to an instance of data 我有一个ListView绑定到数据实例

ObservalbeCollection<ActivityItem> ActivityItems

            <ListView
                x:Name="ActivityItemsList"
                ItemsSource="{Binding ActivityItems}"
                ItemTemplate="{StaticResource Herke80ItemTemplate}"
                Header="{Binding DateFilterListBox.SelectedItem}" />

When I run a filter, I want to select the ListViewItem bound to the ActivityItem within the ListView, and change its visibility depending on the filter selected. 运行过滤器时,我想选择绑定到ListView中ActivityItem的ListViewItem,并根据所选过滤器更改其可见性。

I was doing this by keeping another ObservableCollection instance, meaning the data instance was duplicated. 我这样做是通过保留另一个ObservableCollection实例,这意味着该数据实例已重复。 I then removed or added items accordingly. 然后,我相应地删除或添加了项目。 This took up a lot of loading time. 这占用了大量的加载时间。 So I figured I'd try keep to one binding, and disable or enable the UI elements. 所以我想我会尽量保持一种绑定,并禁用或启用UI元素。

        foreach (ActivityItem activityItem in ActivityItemsList.Items)
        {
            if (activityItem == null) continue;

            var index = ActivityItemsList.Items.IndexOf(activityItem);

            (ActivityItemsList.Items[index] as ListViewItem).Visibility = Visibility.Collapsed;

            int startComparer = DateTime.Compare(activityItem.Start, selectedStartDate);
            int endComparer = DateTime.Compare(selectedEndDate, activityItem.End);

            if (OverdueToggleSwitch.IsOn)
            {
                (ActivityItemsList.Items[index] as ListViewItem).Visibility = Visibility.Visible;
            }
            else
            {
                if (startComparer >= 0 && endComparer >= 0)
                {
                    (ActivityItemsList.Items[index] as ListViewItem).Visibility = Visibility.Visible;
                }
            }
        }

ex is a NullReferenceException, due to the fact that the ListViewItem is not actually a ListViewItem but an ActivityItem. 由于ListViewItem实际上不是ListViewItem而是ActivityItem,因此ex是NullReferenceException。

What is an alternative or the correct way of doing this? 什么是替代方法或正确的方法?

I think you have over complicated it. 我认为您已经使它复杂化了。 If you use Snoop you can see that when you bind a ListView to a collection via ItemSource it ends up populated with a collection of ListViewItems where each ListViewItem has two things. 如果使用Snoop ,则可以看到,当通过ItemSource将ListView绑定到集合时,它最终会填充有ListViewItems的集合,其中每个ListViewItem都有两件事。

  1. a DataContext set to the data (eg ActivityItem) 设置为数据的DataContext(例如ActivityItem)
  2. a ContentPresenter filled with the controls defined in your DataTemplate ContentPresenter,其中填充了DataTemplate中定义的控件

This means that in theory you can browse both collections 这意味着理论上您可以浏览两个集合

from x in listView.Items select x as ListViewItem

or 要么

from x in listView.Items select x.DataContext as ActivityItem

However if you simply want a filtered list then can I suggest changing your binding, 但是,如果您只是想要一个过滤列表,那么我可以建议您更改绑定,

 <ListView
            x:Name="ActivityItemsList"
            ItemsSource="{Binding FilteredItems}"

and

public class MyViewModel :INotifyPropertyChanged    
{
   public IEnumerable<ActivityItem> AllItems {get;set;} //needs to NotifyPropertyChanged(FilteredItems)
   public Func<ActivityItem, bool> Filter { get;set;} //needs to NotifyPropertyChanged(FilteredItems)
   public IEnumerable<ActivityItem> FilteredItems { get { return from x in AllItems where Filter(x) select x; }}
}

If you are still struggling with performance issues then have a quick read through Bea Costa's series on TreeView performance part one , part two and part three . 如果您仍然在性能问题上挣扎,那么请快速阅读Bea Costa关于TreeView性能的第一部分 ,第二部分第三 部分的系列文章。 It might help you to get WPF doing all the hard work for you. 它可以帮助您让WPF为您完成所有艰苦的工作。

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

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