Data Binding to a Dependency Property on WPF ListBox

Problem: I need to display a feed from a certain source in a WPF ListBox.

Approach: Set the item source of the ListBox to a dependency property. As the property changes, the items in the ListBox will also change.

Solution:

1. Create a dependency property and its wrapper CLR property.

            /// <summary>
            /// The Feed dependency property.
            /// </summary>
            public static readonly DependencyProperty FeedProperty = DependencyProperty.Register("Feed", typeof(SyndicationFeed), typeof(FeedViewer));
            /// <summary>
            /// Gets or sets the feed.
            /// </summary>
            /// <value>The syndication feed.</value>
            public SyndicationFeed Feed
            {
                get { return (SyndicationFeed)GetValue(FeedProperty); }
                set { SetValue(FeedProperty, value); }
            }

        Note that FeedViewer is the user control containing the ListBox.

2. Bind the ListBox item source to the property.
    There are two ways (well, at least, I think) to accomplish this.
    2.1 XAML

    <UserControl x:Class="Controls.Wpf.FeedViewer"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              
x:Name="usrFeedViewer">
<Grid>
<ListBox x:Name="uxFeedListBox"                 
ItemsSource="{Binding ElementName=usrFeedViewer, Path=Feed.Items}" HorizontalContentAlignment="Stretch"                 
Grid.Row="1" Margin="12,0,12,12" />
</Grid>
</UserControl>
Note that the ItemSource accepts IEnumerable. Therefore, we assign the feed items for it instead of just Feed.


    2.2 Code Behind
        For this approach, we are to create a PropertyChangedCallback event to handle when the Feed property is changed.
        There is no need to set the ItemSource property in XAML for the ListBox.
        The dependency property will be changed to:

        /// <summary>
        /// The Feed dependency property.
        /// </summary>
        public static readonly DependencyProperty FeedProperty = DependencyProperty.Register("Feed", typeof(SyndicationFeed), typeof(FeedViewer), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnFeedChanged)));

        Now we define the event named OnFeedChanged:

        /// <summary>
        /// Called when [feed changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void OnFeedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var feedViewer = (FeedViewer)sender;
            var newValue = (SyndicationFeed)e.NewValue;

            feedViewer.uxFeedListBox.ItemsSource = null;
            feedViewer.uxFeedListBox.ItemsSource = newValue.Items;
        }

Now when you set/change the Feed property for this control in your application, the ListBox will show you corresponding feed :)

A Clever(?) AdWords Technique

For those of you who read manga, you probably know of the name “Naruto”.
”Naruto” is the name of a quite famous Japanese manga series (read more…).
While browsing through a manga site today, I have come across an AdWords that really caught my attention.
”Naruto Hotels”!!! (What the…). Please see the image below for the ad.
I can see that it is an ad from a trip agency but why Naruto!!!???
After reading the details, I realized that, if I don’t misunderstand, the ad probably takes any famous word and put it as the title even if it doesn’t relate to what it is trying to sell.
The outcome is that the trip agency ad appears on a manga site!

Snap_2009.11.21 14.58.10_001

What do you think about this marketing technique?
Do you think the manga readers are their target audiences?
Is this technique effective and not against any rule?
Will it confuse the Web users seeing the ad?

Buildings For Sale

OK.  This is unusual but, well, here we go!

We’ve got buildings for sale in Chiang Mai, Thailand – the land of smiles :)
Chiang Mai has been one of the best place to live in Thailand and probably in Asia or even the World.
With its unique characteristics and cultures, millions of people around the globe come to visit every year and many of them have even decided to settle down.

Today we offer you a great place with a great deal.
The buildings are located close to a famous shopping mall – Central Plaza, Kad Suan Kaew, and one of the most famous educational institutions in the North – Chiang Mai University.
It also takes no time for you to commute to Chiang Mai International Airport.

3 3.5-story buildings for sale:
The middle two are for sale at 3.49M THB each.
The right one is for sale at 3.79M THB.
(The biggest one on the other side has been sold.)
Dimension: 4m x 12m (48 sq.meters)
backyard: 2 m
footpath area: 1.5 m
parking area (in front of building): 4 m

Nice people, great place, and excellent location!
Isn’t that great?
What are you waiting for!
Call +66 (81) 814-2607 for English
Or +66 (87) 101-9613 , +66 (83) 482-6413, +66 (85) 695-2937 for Thai


View Larger Map