Filtering collections from XAML using CollectionViewSource

by Nico 12. April 2012 17:18

I find that I often run into the need of separating a collection of items into several collections just so I can bind them to multiple listboxes, for example a list of sessions spanning several tracks and each track is shown in his own listbox in a pivotitem. To get this done you can start by adding multiple collections to your viewmodel and divide the items there. However this makes your viewmodel very big in a very short time. A better way to do this is using CollectionViewSource items in XAML. Let me show you how.

First thing I did was building a demo class existing out of a title and a description, these two properties will be shown in the listbox later on. A third property is the one we’ll use to filter the data, here’s the completed class.

public class DemoClass
{
    public string Title { get; set; }
    public string Description { get; set; }
    public Pivot PivotToAppearIn { get; set; }
}

Nothing special here. Notice the Pivot instance, this is just an Enum that will be the way to filter later on.

public enum Pivot
{
    First,
    Second,
    All
}

For the demo’s purpose I’ll be creating a bunch of dummy data in the viewmodel. The project template I’ve used here is the default pivot app template in the Windows Phone 7.1.1 SDK. It comes with a bunch of dummy data, I’ve used the same data but put them in instances of the DemoClass. Those instances are put inside an ObservableCollection.

This is the viewmodel

public class MainViewModel : INotifyPropertyChanged
{
    public const string ItemsPropertyName = "Items";
    private ObservableCollection<DemoClass> items;
    public ObservableCollection<DemoClass> Items
    {
        get
        {
            return items;
        }

        set
        {
            if (items == value)
            {
                return;
            }

            items = value;
            NotifyPropertyChanged(ItemsPropertyName);
        }
    }

    public MainViewModel()
    {
        this.Items = new ObservableCollection<DemoClass>();
        LoadData();
    }

    public void LoadData()
    {
        Items.Add(new DemoClass 
        { 
            Title = "runtime one", 
            Description = "Maecenas praesent accumsan bibendum", 
            PivotToAppearIn = Pivot.First 
        });
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

So we’ve got our basic bindable property here and a method that loads in the dummy data. In the demo project there’s obviously more then one item in the collection, there’s about 16 to be precise.

In the design I didn’t change a lot from the default template. I’ve just copied the ItemTemplate from the listbox to the pageresources so that it can be reused in the second listbox.

This is the template.

<phone:PhoneApplicationPage.Resources>
    <!-- template for the listboxes -->
    <DataTemplate x:Name="ListBoxTemplate">
            <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                <TextBlock Text="{Binding Title}" TextWrapping="Wrap" 
                           Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                <TextBlock Text="{Binding Description}" TextWrapping="Wrap" 
                           Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
            </StackPanel>
        </DataTemplate>
</phone:PhoneApplicationPage.Resources>

All right now that the preparations are set, time to get into the filtering. First step is to add a CollectionViewSource for each listbox. These are set on the same place as I’ve put the listbox ItemTemplate, in the pageresources. For this demo I need two CollectionViewSources.

<CollectionViewSource x:Name="FirstPivot" Filter="FirstPivot_Filter" Source="{Binding Items}" />
<CollectionViewSource x:Name="SecondPivot" Filter="SecondPivot_Filter" Source="{Binding Items}" />

So what’s all this? x:Name is like in any other XAML object, it’s just the name that can be used to reference the object. The source is the ObservableCollection that was created in the viewmodel. And last but definitely not least is the Filter event. This event will fire for every item in the collection that is bound to the Source property.

Now for the event handler, I’ll just post the event handler FirstPivot_Filter here because they are basically the same.

private void FirstPivot_Filter(object sender, System.Windows.Data.FilterEventArgs e)
{
    e.Accepted = (e.Item as DemoClass).PivotToAppearIn == Model.Pivot.First || 
        (e.Item as DemoClass).PivotToAppearIn == Model.Pivot.All;
}

FilterEventArgs has two properties, Accepted is a boolean that when true shows the item in the listbox that is bound to the CollectionViewSource. Item is the current item in the collection. Remember that this event is triggered for each item in the collection. So what we do here is casting the Item property to an instance of DemoClass then check if the PivotToAppearIn property, that was an instance of the enum, is either First or All.

Now that we have the CollectionViewSources and the event handlers in place it’s time to bind the ViewSource to the listbox.

<controls:PivotItem Header="first">
    <ListBox x:Name="FirstListBox" Margin="0,0,-12,0" 
             ItemsSource="{Binding Source={StaticResource FirstPivot}}"
             ItemTemplate="{StaticResource ListBoxTemplate}" />
</controls:PivotItem>

The bindingsource of ItemsSource is bound to the CollectionViewSource that filters for this listbox. And that’s it!

 

In this article I’ve shown how you can filter a collection using a CollectionViewSource in XAML. This is an easy and fast way to visually filter data while keeping a clean ViewModel.

Download the Demo project here.

Tags:

.Net | Binding | MVVM Light | Windows 8 | XAML | WP7 | WP8 | Silverlight | Metro | Devices

Make your WP7.5 app run on low-end devices

by Nico 28. February 2012 20:54

On MWC the first Windows Phone Tango devices were announced. The Nokia Lumia 610 and the ZTE Orbit are low-end devices running on only 256MB of RAM. This is made possible thanks to a new, lower end version of the Windows Phone platform. Developers need to make their applications available for either low-end or high-end devices or both. This is determined by the memory usage of the application and if it’s set in the manifest file. Since the new devices only have 256MB of ram your application can use a maximum of 90MB according to the developer guidelines. The memory consumption of your application can be tested with the marketplace test kit by right clicking on your WP7.5 project in Visual Studio 2010.

The refresh of the WP7.1 SDK includes a second emulator running on the low end version of Windows Phone. With this developers can now test if their applications will keep running on the cheaper devices. If not, the user will get an error when trying to install your application stating that it won’t run on their low-end device. Users with a mid –or high-end device can keep using your app without any problems.

This is in fact the same emulator but running on a different image. After installing the update you won’t be able to launch the emulator from the Windows start menu anymore, to fix this add either –256 or –512 to the shortcut depending on what image you want to load in the emulator.

With that, let’s dive into some code and figure out what we can do on the low-end device.

I’ve created a demo project that on launch checks for the available amount of memory using a Device Extended Property from the new SDK called ApplicationWorkingSetLimit. I’ve poured this into a method that sets a private boolean to true if it’s a low-end device

Code Snippet
  1. private void CheckWP7Version()
  2. {
  3.     //check if the device is low-end
  4.     long result = (long) DeviceExtendedProperties.GetValue("ApplicationWorkingSetLimit");
  5.  
  6.     if (result < 94371840)
  7.     {
  8.         isLowEndDevice = true;
  9.     }
  10.     else
  11.     {
  12.         isLowEndDevice = false;
  13.     }
  14. }

Note that the ApplicationWorkingSetLimit is a hardcoded string, so be very careful of typos. The 94371840 is the maximum amount of bytes my application can use on a low-end device.

Another limitation of Windows Phone Tango is that it currently does not support background agents and that includes both periodic tasks as well as resource intensive tasks. When you try to add a background agent on a low-end device you get this:

That’s not a very clear error message is it? The inner exception doesn’t tell us much more. This will probably change once the new SDK is completely finished. So when you’re using background tasks in your application, please update your app with checks of the version. Don’t submit the update to the marketplace just yet because the current CTP version of the SDK doesn’t have a go-live license so it won’t pass marketplace certification.

My example is an app that will set a random count to the application’s tile if it’s pinned to the start screen. In debug mode it will do this every 30 seconds. When you install it on the 256MB version of the emulator it won’t be able to register the agent, in fact it won’t even try because I have a build-in check for low memory devices.

In my example I’ve used a boolean to store whether or not the device has low memory. This works because my app only has one page. In a real life application it would be better to store this into the IsolatedStorageSettings using this code.

Code Snippet
  1. private void CheckWP7Version()
  2. {
  3.     bool isLowEndDevice;
  4.  
  5.     //check if the device is low-end
  6.     long result = (long)DeviceExtendedProperties.GetValue("ApplicationWorkingSetLimit");
  7.  
  8.     if (result < 94371840)
  9.     {
  10.         isLowEndDevice = true;
  11.     }
  12.     else
  13.     {
  14.         isLowEndDevice = false;
  15.     }
  16.  
  17.     if (IsolatedStorageSettings.ApplicationSettings.Contains("IsLowEndDevice"))
  18.     {
  19.         IsolatedStorageSettings.ApplicationSettings["IsLowEndDevice"] = isLowEndDevice;
  20.     }
  21.     else
  22.     {
  23.         IsolatedStorageSettings.ApplicationSettings.Add("IsLowEndDevice", isLowEndDevice);
  24.     }
  25.  
  26. }

And load the value anywhere in your application again like this

Code Snippet
  1. var isLowEndDevice = IsolatedStorageSettings.ApplicationSettings["IsLowEndDevice"];

If for some reason your application can’t run on low-end devices you can block those devices from downloading it. To do this you need to add some lines to the application’s manifest file, right below the capabilities.

Code Snippet
  1. <Requirements>
  2.   <Requirement Name="ID_REQ_MEMORY_90" />
  3. </Requirements>

With this in place the low-end devices will get a message stating that their device is not suited to run your application.

I hope I gave you a good overview about what to expect with the new low-end devices and that you can adjust your applications to support these devices. Don’t be too worried about the memory limitations on the new devices, Microsoft scanned all available applications and found only 5 that utilise too much memory, they’ve contacted the developers and are working together with them to overcome these problems.

The solution of my example can be found here(SkyDrive link).

Tags:

XAML | WP7 | Metro | Devices | .Net

Techdays Belgium 2012

by Nico 2. February 2012 08:27

So Techdays is right around the corner and I’ll be attending for the third time.I’ll be attending all kinds of sessions going from Windows 8 development to the complete deep dive track on web to my favorite subject, Windows Phone 7 development. I’m also excited about the Scott “The Gu” Guthrie doing the opening keynote and doing a session the second day. Also presenting this year is Laurent Bugnion, the father of the awesome MVVM Light framework.

Here’s the list of sessions I’ll be attending, this list is subject to change depending on if I change my mind the last minute, as I’m known to do sometimes Glimlach.

Tuesday February 14th

  • Opening keynote with Scott Guthrie
  • Welcome to the Metro Application Platform
  • Windows Phone Fast App Switching, Tombstoning and Multitasking
  • The Future of C# and Visual Basic
  • Devices + Cloud: Using Azure on iOS, Android, Windows Phone, …

Wednesday February 15th

  • ScottGu unplugged
  • Take a ride on the Metro
  • The zen of async: Best practices for best performance
  • MVVM Applied: From Silverlight to Windows Phone to Windows 8
  • MVVM & WCF RIA Services: an architectural story
  • Building a data intensive application

Thursday February 16th

This is a deep dive day, I’ll be following the web track that focuses on what’s new in ASP.net 4.5 and Visual Studio 11

 

Tags:

.Net | Presenting | WP7 | Windows programming | XAML | XNA | Web development | MVVM Light | Devices

FollowMyFeed app submission: the result

by Nico 21. December 2011 18:51

Yesterday, a few days after I submitted my app that was generated by FollowMyFeed (see previous blog post) I got the result. The app passed certification and is now available on the marketplace. Hooray!

But there’s one thing that’s kind off weird. The marketplace certification rules clearly say that every app should launch and be ready to use in under 5 seconds. The generated app takes 7-8 seconds on my Omnia 7. I can think of two reasons why the app still passed certification.

  • The test team only used second generation devices and they boot up the apps faster
  • They don’t care since FollowMyFeed is a Microsoft product and it would be bad for them if they shot down apps generated by their own software.

Anyhow, now you can have an app to read my blog and follow my Twitter feed. Get the app here

Tags:

.Net | WP7 | XAML

FollowMyFeed: generate WP7 apps without any coding

by Nico 15. December 2011 16:11

Some time ago MIcrosoft launched a website called FollowMyFeed. The site is a service that allows anyone with an RSS feed to generate an application for Windows Phone 7 in mere minutes, without any coding. Sounds like fun, so I decided to test the service with the RSS from this blog.

These are the features, copied from the site:

  • Multiple RSS feeds
  • Logo, Tile, Background, Pivotbackground custom
  • Offline content
  • Comments (online)
  • Share to social networks (online)

Before you get started you do need to prepare some graphics like a splash screen, background and icons. For this purpose Microsoft put some templates on FollowMyFeed in both Photoshop and Paint.Net format. The templates are in a zip file and contain all the graphics needed to build an app. All graphics accept all the different icon sizes that are needed, luckily there’s an app for that. Someone build an app that takes an image, you select a square that you want used as icons, click save and the app will save the icon in all the necessary sizes. WP IconMaker can be found here (codeplex).

Generating a new application is as easy as following a small wizard, selecting the correct graphics and colors, setting the RSS feed and you’re done. I did encounter 1 issue, when I uploaded the splash screen it didn’t work. A splash screen can be maximum 480x800 pixels, the resolution of all WP7 devices, my splash screen was exactly that size but didn’t work. I’ve resized it to 479x799 and it worked flawlessly. Besides this small bug I didn’t encounter any problems, I’ve downloaded the .XAP file, installed it onto the emulator to take some screenshots and submitted it to the marketplace, once I got the result of my submission, I’ll let you know.

I made some screenshots of the entire process, those can be found here

Tags:

.Net | WP7 | XAML

Binding a dynamic Pivot in Windows Phone 7

by Nico 13. December 2011 13:30

I’m currently working on a Windows Phone 7 application that needs a dynamic Pivot. For every item in a list there should be a pivot item. Besides that, every dynamic pivot item should have a listbox that is bound to another list. I found very little information on these topics so I decided to write it down myself.

Both the header binding and the listbox binding are done from the same class. The class is called DemoClass and contains a string Name that will be bound to the header of the pivot item and a list of strings called Result that will be bound to the pivot item’s body. Here’s the code for the class:

Code Snippet
  1. using System.Collections.ObjectModel;
  2.  
  3. namespace BindingDynamicPivotDemo
  4. {
  5.     public class DemoClass
  6.     {
  7.         public string Name { get; set; }
  8.         public ObservableCollection<string> Result { get; set; }
  9.  
  10.         public DemoClass()
  11.         {
  12.             Result = new ObservableCollection<string>();
  13.         }
  14.     }
  15. }

I used ObservableCollection here instead of List because ObservableCollection already implements INotifyPropertyChanged and takes care of notifying all it’s subscribers when it changes. It’s perfectly possible to use a IList<string> instead of the ObservableCollection but then the DemoClass needs to implement INotifyPropertyChanged. In the constructor of the class the collection gets initialized.

Next thing I needed was a viewmodel to bind to my xaml page. The viewmodel declares a collection of DemoClass instances and fills those instances with dummy data.

Code Snippet
  1. using System;
  2. using System.Collections.ObjectModel;
  3.  
  4. namespace BindingDynamicPivotDemo
  5. {
  6.     public class MainViewModel
  7.     {
  8.         public ObservableCollection<DemoClass> PivotItems { get; set; }
  9.         
  10.         public MainViewModel()
  11.         {
  12.             PivotItems = new ObservableCollection<DemoClass>();
  13.  
  14.             //load pivot headers
  15.             LoadData();
  16.  
  17.             //load random dummy data for the pivot body
  18.             FillLists();
  19.         }
  20.  
  21.         public void LoadData()
  22.         {
  23.             //pivot headers
  24.             for (int i = 1; i <= 10; i++)
  25.             {
  26.                 DemoClass newClass = new DemoClass {Name = "pivotItem " + i};
  27.  
  28.                 PivotItems.Add(newClass);
  29.             }
  30.         }
  31.  
  32.         public void FillLists()
  33.         {
  34.             Random rnd = new Random();
  35.  
  36.             //fill each list with dummy data, this will be shown in the pivot body
  37.             foreach (DemoClass pivotItem in PivotItems)
  38.             {
  39.                 pivotItem.Result.Clear();
  40.  
  41.                 for (int j = 0; j < 10; j++)
  42.                 {
  43.                     pivotItem.Result.Add("item " + rnd.Next(0, 1000));
  44.                 }
  45.             }
  46.         }
  47.     }
  48. }

I am again using an ObservableCollection for the same reason as I did in the DemoClass. LoadData() creates 10 instances of DemoClass and adds them to the PivotItems collection. FillLists() will fill the Result collection of every DemoClass instance with random dummy data.

Next step is to let the view know where it needs to look for its data. This can be done from xaml or from code behind. MVVM Light takes the xaml approach while the default Visual Studio projects do it from code behind, since this is a default project I followed the code behind approach. This is how my MainPage.xaml.cs looks liks.

Code Snippet
  1. using System.Windows;
  2. using Microsoft.Phone.Controls;
  3.  
  4. namespace BindingDynamicPivotDemo
  5. {
  6.     public partial class MainPage : PhoneApplicationPage
  7.     {
  8.         // Constructor
  9.         public MainPage()
  10.         {
  11.             InitializeComponent();
  12.  
  13.             // Set the data context of the listbox control to the sample data
  14.             DataContext = App.ViewModel;
  15.         }
  16.  
  17.         private void Button_Click(object sender, RoutedEventArgs e)
  18.         {
  19.             App.ViewModel.FillLists();
  20.         }
  21.     }
  22. }

Instantiation of the ViewModel occurs in App.xaml.cs and is auto-generated code. The Button_Click method is an event handler for a button, obviously. It will call a method on the ViewModel that regenerates new random dummy data for the pivot body. This shows how an ObservableCollection gives a powerful auto-updating binding while keeping the DemoClass nice and clean.

Now let’s take a look at the xaml and specifically at the bindings. This is the MainPage.xaml.

Code Snippet
  1. <Grid x:Name="LayoutRoot" Background="Transparent">
  2.         <!--Pivot Control-->
  3.         <controls:Pivot x:Name="PivotPlatform" Margin="0,0,8,77" Title="BindingDynamicPivotDemo" ItemsSource="{Binding PivotItems}" >
  4.             <controls:Pivot.HeaderTemplate>
  5.                 <DataTemplate>
  6.                     <TextBlock Text="{Binding Name}"/>
  7.                 </DataTemplate>
  8.             </controls:Pivot.HeaderTemplate>
  9.             <controls:Pivot.ItemTemplate>
  10.                 <DataTemplate>
  11.                     <ListBox ItemsSource="{Binding Result}">
  12.                         <ListBox.ItemTemplate>
  13.                             <DataTemplate>
  14.                                 <TextBlock TextWrapping="Wrap" Text="{Binding}" />
  15.                             </DataTemplate>
  16.                         </ListBox.ItemTemplate>
  17.                     </ListBox>
  18.                 </DataTemplate>
  19.             </controls:Pivot.ItemTemplate>
  20.         </controls:Pivot>
  21.         <Button Content="Randomize" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,0,4" Click="Button_Click"/>
  22.     </Grid>

I only pasted the layoutRoot grid here because all the xml namespaces are the default ones from a WP7 project. So for starters I’ve bound the Pivot’s ItemsSource to the PivotItems collection. Then I declare two templates, the header template is the title of the pivot item, by binding it to name it will use the Name property from the DemoClass. Since the Pivot’s ItemsSource is bound to PivotItems, the textblock in the header template is now bound to PivotItems.Name. The itemtemplate is what makes up the pivot’s body, it contains a listbox that is bound to PivotItems.Result. Underneath the pivot is a button that triggers the event handler in MainPage.xaml.cs.

When this is executed you’ll see an automatically generated pivot bound to dummy data, every time the button is clicked new data will be generated and shown in the pivot immediately thanks to the ObservableCollection.

screen

The source of this project can be found here.

Conclusion

In this small article I explained how you can databind a pivot so that it’s items are automatically generated and bound to other properties. It’s not hard to do but it took me some time to figure out so I hope I’ve helped someone by writing down my findings.

Tags:

.Net | WP7 | XAML | Binding | Silverlight

About the author

Hi,

My name is Nico, I’m an MCP living in Belgium.
I’m currently employed as a .Net Software Developer at RealDolmen, one of Belgium’s leading IT single source providers.

I'm also founding member and board member of the Belgian Windows Phone User Group. If you're in Belgium feel free to drop by if we're doing an event. http://www.wiphug.be

This blog will be about Windows Phone 7, C#, XNA , WPF, Silverlight, and much more!

I hope to get feedback from my readers either through comments, mail (nico_vermeir@hotmail.com), twitter, facebook, …

 

Month List