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?

Hit Enter Key in a TextBox to Submit a Form – ASP.NET

In ASP.NET, by default, hitting the Enter key in a TextBox fires the Click event of the first button found in the form.
For example, you have sub-forms within a <form> element and each form requires its own default button.
The default action of firing the first button found on a Web page is not what we want.
Making the <form> run at server and setting a DefaultButton property for the form is not a solution since it can only assign one button as the default button.
Having more than one server-side forms is not a solution either since it will not work.

One solution is to surround each sub-form with <asp:Panel> element and setting its DefaultButton property to a desired button.
This way, you can have many sub-forms on one page with their own default buttons :)

Enable Firefox to Access a SharePoint Site Without Logging In

By default, you will need to provide a username and password for accessing a SharePoint site via Firefox even though you are in the same domain as SharePoint’s.
This does not happen in IE.
To enable an automatic login for Firefox, you will need to configure Firefox to use Windows’s built-in NTLM authentication.
To do so, follow the steps below:

  1. Type about:config in Firefox’s address bar and press Enter key.
  2. In the Filter textbox, type network.automatic-ntlm-auth.trusted-uris and press Enter key.
    You should now see the specified setting appearing in the preference name section.
  3. Double click the preference name (or right click and choose Modify) to modify the value.
  4. In the appearing dialog, enter the SharePoint server URLs (with no tailing “/”) separated by a comma and click OK when done.

That’s all and you are good to go :)

A Class in App_Code Folder Does Not Get Compiled

Today I’ve experienced one odd thing with Visual Studio 2008.
As we know, App_Code is not needed for a Web application project under VS2008 since class files can be put anywhere.
However, to keep it clean and easy to find, I decided to created one and create class files under the folder.
It appears that those created objects do not be available.
In the other words, they do not get compiled.
After messing around, I found that the “Build Action” property of those files have been set to “Content” by default.
Well, it should be “Compile” so that the files get compiled.
I’ve also tried to create another folder with the name of “cs”.
The created class files under “cs” have the “Build Action” property of “Compile”.
Therefore, the solution is either set the “Build Action” property to “Compile” or never use a folder with the name of “App_Code”.

Now the question is… why does the “Build Action” property of the files under “App_Code” is “Content”?

Case-Insensitive XPath Searching

As far as I know, there is no such xpath function to do the case-insenstive search.
However, there is a work-around for this.
The solution is to receive a lower-case string as the input and use lower-case() xpath function to help perform the search.
Consider the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<specification>
  <name>ASUS Striker</name>
  <manufacturer>ASUS</manufacturer>
  <model>Striker</model>
</specification>

The following xpath do the search for “asu” which returns two string – “asus striker” and “asus”:

/specification//lower-case(text())[contains(.,'asu')]

As we can see, using lower-caes() and contains() helps search for lower-case string even though the xml contains upper-case string.
The text() function indicates the value of the node in context.
You can also try name() which indicates the name of the node.
You can try using upper-case() with upper-case input string as well.

VMWare Workstation on Windows Server 2008

I’m currently in a progress of deciding whether to VMWare Workstation or Hyper-V on my Windows Server 2008 for a development environment.  At first, I meant to seek for the one with better performance.  However, I’ve come across one article at diTii.com about an issue on using VMWare Workstation on Windows Server 2008.  Below is what they say.  It’s good to know this stuff beforehand.  Thanks :)

Due to the fact, in the current Intel/AMD chip architecture, only one hardware-based hypervisor can run at a time, you will want to create a special boot entry for a Hyper-V-less boot time configuration of Windows 2008. Assuming you are currently booted into Windows 2008, at an administrative command prompt, type the following:

bcdedit /copy {current} /d “Windows 2008 (No Hyper-V)”

The above command should say: The entry was successfully copied to {guid}.

Copy that {guid} to the clipboard including the curly braces.

Now, type the following command:

bcdedit /set {guid} hypervisorlaunchtype off

In the above command, replace {guid} with what you into the clipboard.

Boot into the ‘Windows 2008 (No Hyper-V)’ instance and you will no longer bluescreen while running VMWare guests.

Space Under An Image in IE

When adding an image in a <div> (as shown in the HTML code below), IE displays a little white space below the image.  This doesn’t happen on Firefox though.

<div>
    <img alt="Pronto Marketing – How We Help You" src="/img/home_img2.png" />
</div>

stolenbit_space_under_image_ie

The fix is simple, just put the closing div tag </div> on the same line as the image and the problem is fixed:

<div>
    <img alt="Pronto Marketing – How We Help You" src="/img/home_img2.png" /></div>

stolenbit_space_under_image_ie_removed

Tell Your DotNetNuke Module Setting Page To Load Module.css

By default, module.css does not get loaded for a module’s setting page.  To let it load the file, add the following code (in C#) to your module setting code behind file (settings.ascx.cs):

protected void Page_Load( object sender, System.EventArgs e ) {
    DotNetNuke.Framework.CDefault DefaultPage = (DotNetNuke.Framework.CDefault)Page;
    DefaultPage.AddStyleSheet( "ModuleSettingStyles", string.Format( "{0}/module.css", this.ModulePath ), true );
}

Debugging a DotNetNuke Compiled Module in VS2008

If you are using VS2008 to create a DNN module with a compiled module starter kit, one option is to attach the project to w3wp.exe process.  You can access it via Debug > Attach to Process… menu.

stolenbit_attach_to_w3wp

If attaching the project to w3wp.exe doesn’t seem to be an option somehow (e.g. you just cannot find it), what you need to do now is to modify some project settings as shown below.

In my case, I’ve create a host record with local.dotnetnuke as the host name.  In your case, you may want to replace http://local.dotnetnuke with http://localhost/dotnetnuke, for example.  Please note that I’ve saved my project under /DesktopModules/ContactUsForm.  So my project url becomes http://local.dotnetnuke/DesktopModules/ContactUsForm.

stolenbit_debugging_dotnetnuke_compiled_module_vs2008

Save the project and try running it again.  You should now be able to debug the project Wink