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 :)

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”?

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