WPF ListBox and large amound of data - c#

I need to display a large amount of data (thousands of items) in a ListBox however it takes some times and the UI is not responsive until the whole items are displayed in the ListBox.
The ItemsSource of the ListBox is bound the an CollectionView.
I know the benefit of using the VirtualizingStackPanel but I insist to use a WrapPanel.
I've searched over the internet and found some VirtualizedWrapPanels but they have the same issue which is they don't allow the VirtualizingWrapPanel to grow to whatever size it likes and I have to set both Width and Height for them.
Now I need to know what other options are out there for me to do the job? What can I do so that the ListBox loads and displays this large amount quickly.
Please let me know if I haven't explained m
y issue clearly.
Any help is appreciated. Thanks in advance.
Edit
This is the relevant code
public ObservableCollection<T> Items
{
get { return _items; }
set
{
if (_items == value)
return;
_items = value;
OnPropertyChanged(() => Items);
}
}
I instantiate the ObservableColelction in the LoadData method of the ViewModel
_items = _service.Select();
and the CollectionView is instantiated in this way
ICollectionView cv = new CollectionViewSource() { Source = Items }.View;
and the xaml code of the ListBox
<ListBox
x:Name="Items"
ItemsSource="{Binding CollectionView}"
Padding="10,10,10,10"
SelectionMode="Single"
VerticalAlignment="Top"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" Height="480" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>

I am not sure if this is applicable or you are looking for this, however virtualizing the data is one more solution you can look into. there are some very good posts describing this. these 2 blog/post seems to be very good. Please have a look Here[http://www.codeproject.com/Articles/34405/WPF-Data-Virtualization] and here[http://www.zagstudio.com/blog/498#.U7a6OvldV1F].

Related

Disable certain ListViewItem depending on custom property UWP

I have a ListView that contains several types of custom UserControls.
The project requires that some of them must be non-clickable, so I would like to disable them, but JUST THEM.
Those items will be enabled/disabled depending on the value of a custom property.
I've tried to set the ListViewItem.IsEnabled property to false, but it ain't worked, and the other solutions I've found around make no sense to me...
I let a sample of the code:
XAML
<ListView x:Name="homeLW"
Margin="0,5,0,0"
ItemClick="homeLW_ItemClick"
IsItemClickEnabled="True"
HorizontalAlignment="Center"
ItemsSource="{Binding Source}">
Where Source is a ObservableCollection<UserControl>.
The problem is that I can't get the items of the ListView as ListViewItems, but as the UserControl type:. When executing this:
foreach(ListViewItem lwI in homeLW.Items)
{
//CODE
}
I get:
System.InvalidCastException: Unable to cast object of type
UserControl.Type to type Windows.UI.Xaml.Controls.ListViewItem.
Anyone know how could I make it?
Thanks in advance :)
foreach(var lwI in homeLW.Items)
{
ListViewItem item =(ListViewItem)homeLW.ContainerFromItem(lwI);
item.IsEnabled = false;
}
When on load all ListViewItems wont be loaded because of Virtualization. So you get Null when try to get container from item. Workaround would be switching off the virtualization. But it will have performance effects. Since you confirmed that it wont be having more than 20 items,I ll go ahead and add the code
<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
To add onto LoveToCode's answer, if you want to disable the selected items on load and not turn off Virtualization, you'll need to fire the code when the UIElement is loaded. Otherwise, you'll get a System.NullReferenceException. The reason for this is because the Framework Element hasn't been loaded to reference the ListView Container.
homeLW.Loaded += DisableSelectedItemsOnLoad()
private void DisableSelectedItemsOnLoad()
{
foreach(var lwI in homeLW.Items)
{
ListViewItem item =(ListViewItem)homeLW.ContainerFromItem(lwI);
item.IsEnabled = false;
}
}

Windows phone proper way to display collection of items

I have XML file with my items and I'm deserializing them into ObservableCollection<UserControl> and I want to put it to my ItemsControl with my own layout.
Now I've created my own UserControl to handle deserialized data to layout and ObservableCollection is binded to my ItemsControl but if I want to display more than 5-10 items my app if getting unresponsive for a while.
How can I avoid freezes? Should I use DataTemplate within ItemsControl or any other ideas? I'm wondering how its done in apps like twitter or reddit which have many entries and everything is working quite nice. Have already searched for reddit/twitter app source to look how they have implemented it, but without success.
EDIT:
xaml
<ScrollViewer>
<ItemsControl x:Name="items" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
c#
public MainPage()
{
this.InitializeComponent();
items.DataContext = _items;
}
public ObservableCollection<my_item> _items = new ObservableCollection<my_item>();
adding items
for (int i = 0; i < 40; i++)
{
_items.Add(new my_item());
}
my_item is my own UserControl with 2 small images, few buttons and a single TextBlock.
You should use ListView which has a virtualizing panel for displaying items by default:
<ListView ItemsSource={Binding Items} >
<ListView.ItemTemplate>
<DataTemplate>
<!-- here goes your item layout -->
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Maybe one reason for your app "hanging" is if you are deserializing/parsing your XML file on the UI thread, especially if the XML file is large, so the solution to this problem is that you need to do the XML parsing on a background thread:
Task.Run(() =>
{
var items = ParseXML();
});
After edit:
1)
Don't wrap an ItemsControl inside a ScrollViewer, because you will break the virtualization. The ItemsControl itself has its own ScrollViewer so there is no need of an additional one.
If you are on Windows Phone 8.1 Runtime, use ItemsStackPanel instead of VirtualizingStackPanel.
2)
It is recommended for lists with many items to use DataTemplates instead of filling it with UI elements. So rather create a data template from your user control:
<DataTemplate>
<MyUserControl/>
</DataTemplate>

How to bind my ObservableCollection two way to a ListView Extension WinRT Xaml Toolkit

I have a Windows 8.1 application with a ListView and I am using ListViewExtensions from WinRt Xaml Toolkit(Obtained latest from Nuget) to bind BindableSelection
Here is my XAML
<ListView
ItemsSource="{Binding AllItems}"
SelectionMode="Multiple"
ext:ListViewExtensions.BindableSelection="{Binding SelectedItems, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
In my ViewModel I have the following ObservableCollection which I have bound my xaml to
private ObservableCollection<string> _SelectedItems;
public ObservableCollection<string> SelectedItems
{
get { return _SelectedItems; }
set
{
if (value != _SelectedItems)
{
_SelectedItems = value;
NotifyPropertyChanged("SelectedItems");
}
}
}
I have put breakpoints on the get and set of my ObservableCollection. The get will be called as soon as my View loads, but the set is never called even though I select multiple items of my ListView.
Am I doing something wrong.
I would be very glad if someone can point me in the right direction.
Thanks in Advance.
Realized my mistake. I have never created the object for ObservableCollections SelectedItems.
One should create the object for the ObservableCollection at some point otherwise the XAML will be binding to a null object reference which obviously cannot be updated.
Here is how you instantiate the ObservableCollection.
SelectedItems = new ObservableCollection<MyItems>();
However I am still unable to hit the breakpoint of the set function of the ObservableCollection. I believe that this is the default behavior of the Observable. Would be glad if someone can comment on that.
Nevertheless the problem for this particular question is solved.
Thanks

CheckBox Binding Slow

I'm in a bit of a bind here (no pun intended); I have a large collection of view models (500+) which are displayed using an ItemsControl with a WrapPanel as the ItemsPanelTemplate. Each of these view models exposes a Boolean? whose value is bound to the IsChecked property of a CheckBox on the user interface.
The problem is this... whenever I attempt to update all the checkboxes at once it is horrendously slow. Almost 10 seconds to update a list of 500 items. If I run the updating code in a seperate thread I can almost watch the checkboxes be updated one by one.
Can anyone enlighten me as to why this process is so slow and how I could improve it?
I have considered that perhaps the non-virtualizing nature of the WrapPanel could be the guilty party. However, when I bind to the IsEnabled property instead of IsChecked I see an interesting result; namely that changing the value of IsEnabled to true is slow as expected, but changing to false happens instantaneously. This makes me suspicious that the checkbox animation is at fault because as far as I can tell visually there is no animation when disabling a checkbox, but there is when enabling. Profiling has revealed that the vast majority of time is spent in the PropertyChangedEventManager.OnPropertyChanged() method.
Example code below, I'm unfortunately forced to use .NET 3.5:
XAML:
<ItemsControl ItemsSource="{Binding ChildItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type SampleViewModel}">
<CheckBox IsThreeState="True" IsChecked="{Binding Path=IncludeInPrinting, Mode=OneWay}" />
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
ViewModel:
public class SampleViewModel : INotifyPropertyChanged
{
private Boolean? _includeInPrinting;
public Boolean? IncludeInPrinting
{
get
{
return _includeInPrinting;
}
set
{
if (_includeInPrinting != value)
{
_includeInPrinting = value;
RaisePropertyChanged(() => IncludeInPrinting);
}
}
}
}
Slow Code:
foreach (SampleViewModel model in ChildItems)
{
model.IncludeInPrinting = false;
}
EDIT: For what it's worth I'm also seeing a spike in memory usage whenever I check all or uncheck all the checkboxes. ~10MB
EDIT: The performance analysis below seems to confirm that animation is definitely the issue.
I would look at the following control which is open source on CodePlex..
http://virtualwrappanel.codeplex.com/ (Note: I have no affilication with Virtualizing Wrap Panel)
Due to the large number of view models you are working with this would drastically improve performance for you.

How to populate a list box that is in an itemtemplate

I am just learning XAML and programming for Windows Phone 7.
Im trying to create an itemtemplate for a WP7 Pivot Control. I was able to make a template which contains a listbox. Is it possible to access this listbox in the code-behind so I can fill it based on a collection of a custom class? Basically how it works is that I have a pivot control and each item in that control is a category. For each category thatis added, there is a list of items that belong to that category. I need to be able to populate the list on each pivot item with items of that category.
I searched for ideas on how to accomplish this, and I get a lot of examples on databinding, but Im not too familiar on how databinding works in XAML.
Would databinding be the way to go or can I somehow get a reference to the listbox and add the items myself?
Any help would be greatly appriciated!
Thank you
I've some considerations on subject:
1) If you fill Categories list via binding, then you don't have entry point where binding is guaranteed comleted (because binding executes in deferred fashion).
2) Working with ItemTemplate's content is more tricky and unreliable, than DataTemplate approach, and you should use it just in exclusive situations. LogicalTreeHelper and VisualTreeHelper classes will help you.
3) But I would recommend you to build your view based on DataTemplates as it is common practice in WPF. Do you really think that this code is pretty complicated?
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Window.Resources>
<DataTemplate x:Key="InnerItemDataTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
<DataTemplate x:Key="CategoryDataTemplate">
<StackPanel>
<ListView ItemsSource="{Binding InnerItems}"
ItemTemplate="{StaticResource InnerItemDataTemplate}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding Categories}"
ItemTemplate="{StaticResource CategoryDataTemplate}"/>
</Grid>
public class Category
{
public IEnumerable<InnerItem> InnerList
{
get{/*...*/}
}
}
class InnerItem
{
public string Name
{
get{/*...*/}
}
}
public class SampleModel
{
public IEnumerable<Category> Categories
{
get {/*...*/}
}
}
IF you create a new "Windows Phone Pivot Application" the default code shows an example of this but reuses the same items in the listbox in multiple pivotitems.
Here's an overview of what that sample code is doing and how you might go about changing it.
In the constructor of MainPage, the DataContext is set to an object (App.ViewModel).
This Loaded event of MainPage ensures that App.ViewModel is populated.
App.ViewModel is an instance of MainViewModel.
MainViewModel contains an ObservableCollection called "Items". It is this that is bound to an idividual ListBox in a PivotItem:
<controls:PivotItem Header="first">
<ListBox ItemsSource="{Binding Items}">
...
</ListBox>
</controls:PivotItem>
Within the ListBox, you can refer to the contents of the "Items" collection.
If you wanted to adjust this to have different collections for each ListBox/PivotItem you could just adjust this to have multiple collections in the MainViewModel.
HTH.

Categories