Twoway databinding to a MongoDB collection in WPF

I’ll show how to have a two-way databinding between a templated listbox and a MongoDB collection.
I’m finally got around toying with MongoDb.What I’ll show next might not be the most correct way, but it works for me.

Setup

I have a Listview defined in XAML with a datatemplate:

        <ListBox Name="mylistbox">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Naam, Mode=TwoWay}"></TextBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

I created an Entity class (can be a composite if you wish) which is is simple POCO with INotifyPropertyChanged implented. This Entity class well be serialized to my MongoDB collection as-is. Explained here, for example:

        public class Entity
        {
            public string Naam { get; set; }
        }

Retrieve the data and databind

Next up, we need to retrieve the data using a query (coll being my collection a retrieved earlier on (not shown)):

var query = coll.AsQueryable<Entity>().ToList();

The ToList part is important.This will ensure that we get a simple list of Entity objects as our ItemSource and not an IQueryable, otherwise the next part won’t work.

Write changes to database

You can now edit your data in your listbox and once you are ready, you can update all the changes to your MongoDB collection:

            foreach (var item in mylistbox.ItemsSource)
            {
                coll.Save(item);
            }
Advertisements

Using delegates, func and lambdas: a tutorial with soldiers

In this tutorial, written for beginning programmers, I’d like to show a little demonstration on the usage of delegates and how we can go all crazy by refactoring and magically see all our duplicate code disappear.

Imagine we are writing the next ultimate Command&Conquer spinoff which can run on any computer …in console-mode. Read more of this post

Writing a WP7 website scraper application

In this tutorial I will explain how you write a WP7 application using the HtmlAgility Pack in order to use information scraped from a website.
Website scraping is the act of retrieving information from a website page. An act by some considered stealing, by others borrowing. Let’s leave that debate to the others. In this post I will show how easy it is to scrape content from a website so that you can (re)use it in your Windows Phone 7 application. As it is, this information will for the most part also work in other, non WP7, projects of course.
Sometimes website scraping is the only means available to consume certain information from a website. If the website doesn’t have some publicly available API or web service you can use you’re pretty much left with scraping, whether you like it or not.

Read more of this post

Using enum flags to write filters in Linq

Here’s the premise. Suppose we have a cookbook that contains all possible recipes with their names and needed ingredients. Using Linq it should be easy to query the cookbook and, for example, only show those recipes you can make given one or more ingredients you have at your house. My solution is a) extremely nice or b) very strange, bloated and way wrong. I leave it up to you and make sure to message me if you have a much nicer solution…because franky, I’m not a big fan of what follows (I have the itching sensation that using the correct binary logic I can solve this problem much easier…).

Read more of this post