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

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

Cannot Resolve Collation Conflict for Column n in SELECT Statement

When creating a stored procedure and it contains a SELECT statement, you may get the following error message even if the collation of your database seems to be correct.

Cannot resolve collation conflict for column n in SELECT statement.

where n is the column number.

This can be easily fixed by appending the following portion of SQL to the column n in your SELECT statement:

COLLATE <your database collation name>

For example, COLLATE SQL_Latin1_General_CP1_CI_AS.

The database could not be exclusively locked to perform the operation

When trying to change the collation of a database, you may get the following error message:

The database could not be exclusively locked to perform the operation.

To workaround this problem, we first need to set the database for single user.  And change it back for multi user after changing the collation.

ALTER DATABASE <database_name> SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE <database_name> COLLATE <target_collation_name e.g. SQL_Latin1_General_CP1_CI_AS>
ALTER DATABASE <database_name> SET MULTI_USER

Access to the Path... is denied

When your ASP.NET application tries to access a file in a folder, you may get an error message saying "Access to the path <file path> is denied.".  To solve this problem, you will need to provide permissions for NETWORK SERVICE (Windows 2003/2008) or ASPNET (Windows XP) account to access the folder containing that file.  To provide the permissions, do the following:

  1. Right click on the folder and select Properties.
  2. Go to Security tab (if it doesn't appear, see below) and click on the Edit... button under Group or user names section.
  3. Click on the Add... button and, on the popup window (titled Select Users or Groups), click on the Advanced... button.
  4. Click on the Find Now button.
  5. Select NETWORK SERVICE (or ASPNET) account and click OK twice to go back to the permissions window.
  6. On the Permissions window, select the account and check Allow for Full Control.
  7. Click OK twice to close the Properties window.

Now your ASP.NET should have an access to the folder.

What if the Security tab doesn't show up?

You'll need to uncheck the Simple File Sharing option in folder options.

  1. On the Tools menu of your windows explorer, select Folder Options.
  2. On the View tab, scroll down until you find the option saying "Use Simple File Sharing (recommended)".
  3. Uncheck it and click OK to accept the change.

Now the security tab should appear and you can start configuring the permissions as stated above.

Two Great Firefox Extensions for RSS and Google Bookmark

The first extension I would like to recommend is RSS Ticker.  As you may have already known, Firefox can act as an RSS feed reader and you can just bookmark a feed as if it were a Web page.  RSS Ticker displays and scrolls a list of those feeds right under your menu bar.  Go ahead and give it a try.

stolenbit_view_feed_xml

 stolenbit_rss_subscribe

stolenbit_display_in_ticker

That's it.  When there is a new post, you'll get notified right on your Firefox Hot

Another extension that I've found to be very very useful is Google Bookmark IncSearch.  As you browse across the Web and find some useful pages that you want to keep, one way is to bookmark it on Google Bookmark so that you can access your bookmarks from anywhere.  However, sooner or later, you will find yourself store TOO MANY bookmarks and just you don't seem to find what you want within your own bookmarks - even though you've well organized them.  And you probably end up with googling for what you want again -*-  Well, it would be nice if you can just search within your bookmarks if you know it IS there, wouldn't it?  Google Bookmark IncSearch can help.  OK.  That's enough for saying.  Give it a try and I'm sure you'll love it just as I do Happy

 stolenbit_google_bookmark_incsearch

Enable Remote Connection on SQL Server 2008

By default, Microsoft SQL Server 2008 doesn't allow any remote connection.  What we'll need to do is to enable TCP/IP protocol in SQL Server Configuration Manager > SQL Server Network Configuration > Protocols for <InstanceName> and enable the TCP/IP protocol as shown in the picture below.  An example of InstanceName is MSSQLSERVER.

stolenbit_enable_remote_connection_on sql_server_2008

After restarting SQL Server (InstanceName) service (as shown below), it should work without any problem.  You can quickly access the services windows by going to start menu > run > type services.msc.  Locate the SQL Server (InstanceName) service, right click on it, and choose restart.

stolenbit_sql_server_service

In my case, completing these steps enables me to connect from a remote machine successfully.  However, someone reported that we also need SQL Server Browser service running too.  Therefore, if completing the steps above doesn't help you get connected, you may try setting SQL Server Browser service to Automatic and start the service.

Failed to map the path..."

When you create a new .NET Web application under a default Web site in IIS 7 (so the URL of your new Web application will be http://localhost/mywebsite, for example), you may encounter an error saying "Failed to map the path...".

To get rid of this error, make sure that the application pool of your new .NET Web application and the application pool of the default Web site are the same.  The following pictures show the cause of the error:

default_web_site

new_web_app

Change the application pool of the latter one to be the same of the first one solves the problem Angel

This row already belongs to another table."

When you try to add a DataRow from one table to another, you may encounter the following error message:

"This row already belongs to another table."

This error message appears if you try to add (copy) a DataRow from table1 to table2, for example, with the following C# code:

table2.Rows.Add(row);

where row is a DataRow object from table1.

To successfully copy the row, you'll need to use ImportRow() method:

table2.ImportRow(row);

Don't forget to define a proper value of column number for table2 first or no data will be displayed if you bind table2 with a gridview, for example.

WSS 3.0 Security Groups

In WSS 3.0, there are three (3) out-of -box security groups.

  1. Owners (Full-Control)
    • Get full access to the entire site and its subsites.
  2. Members (Read/Write)
    • Have "Contribute" permission level associated.  This "Contribute" permission level gives members quite a bit of power and could be out of control sometimes.
    • Members cannot see "Site Actions" button and those items hidden by an admin.  However, by default, they CAN edit and delete an item created by an admin!!! ("Contribute" permission level)
  3. Visitors (Read-Only)
    • Though visitors cannot see the "Site Actions" button, cannot do site personalization, and cannot edit or delete items, they, by default, CAN modify a shared document -*-
    You can create your own SharePoint groups.
    MOSS 2007 comes with more out-of-box groups.

<< Back to [70-631] TS: Configuring Microsoft Windows SharePoint Services (WSS) 3.0 - Exam Notes

www.localhost.com Nightmare

When you're working with Visual Studio and trying to run a web application project, you may encounter a problem of the URL change from http://localhost to http://www.localhost.com

This is caused by the IPv6 issue.  Look in your hosts file (usually located under C:\Windows\System32\drivers\etc).  Look for the line:

::1    localhost

Comment it out with a # sign and it'll become:

#::1    localhost

That's it. You're now up and running Hot