Posts from July 2009

Jul
31
2009

WPF: Changing Input Focus to Hidden Elements



Posted in Programming and WPF

When trying to change the focused element in the WPF recipe management app I develop, ChickenPing, I recently ran into a problem. I wanted a shortcut key (Ctrl + something) to change the selected tab in a TabControl, then focus an element in that tab. There are a few ways you might try doing this, both do more or less the same thing and both will fail unless the tab in question is already selected. For example:

 tabControl.SelectedItem = someTab;
 someTextBox.Focus();
 FocusManager.SetFocusedElement(this, someTextBox);
 // both only work unless the tab which contains them is visible.

The problem is that an element can only be focused if it is visible. Making the item visible just before trying to focus it fails because the change hasn’t had time to take effect.

Read more »

Jul
29
2009

TopMost Window In XAML



Posted in Programming and WPF

I was kind of looking forward to delving into some native call to make a Window always on top in WPF, but it was actually surprisingly easy, a single binding in the Window’s declaration:

<Window ...
	Topmost="{Binding ElementName=itmTopMost,Path=IsChecked}"
...
	<MenuItem Header="_Always on Top" x:Name="itmTopMost" IsCheckable="True" />

No code involved!

Jul
25
2009

ASP.NET: Remove from the cache using



Posted in ASP.NET and Programming

The Cache object in ASP.NET isn’t like other collections, probably because it needs to be specialized so it doesn’t make use of generics. Unfortunately, that makes it more difficult to work with. This small utility method removes an object from the cache by key using a predictate. You could easily modify it to compare either of the IDictionaryEnumerator’s Key or Value properties.

/// <summary>
/// Removes an item from the cache using a Predictate to match the key.
/// </summary>
/// <param name="keyCriteria">The Predictate to use for each key to determine whether the entry should be removed.</param>
/// <summary>
/// Removes an item from the cache using a Predictate to match the key.
/// </summary>
/// <param name="keyCriteria">The Predictate to use for each key to determine whether the entry should be removed.</param>
public static void RemoveFromCache(Predicate<string> keyCriteria) {
	IDictionaryEnumerator enuma = HttpContext.Current.Cache.GetEnumerator();
	while(enuma.MoveNext()) {
		if(keyCriteria(enuma.Key.ToString())) {
			HttpContext.Current.Cache.Remove(enuma.Key.ToString());
		}
	}
}
Jul
9
2009

ASP.NET: Data Binding with ObjectDataSource



Posted in ASP.NET and Programming

In the past when writing forms with ASP.NET I’ve usually used an SqlDataSource when binding to controls, but I’ve been doing a lot of WPF lately. Being a desktop platform, WPF has excellent support for DataBinding, but obviously since HTTP is a stateless protocol (apart from the ViewState in ASP.NET which isn’t ideal), it’s not so well suited to data binding.

One thing I’ve never really tried is binding an object to a form to display a single item (eg: a report). This leads to writing lots of tedious code like:

	Order theOrder = GetOrder(orderId);
	litName.Text = theOrder.Name;
	litID.Text = theOrder.ID;
	litCustomer.Text = theOrder.BillingCustomer.Name;
	...

Read more »

Jul
5
2009

Faster Image Loading With jQuery



Posted in ASP.NET and Programming

Normally, if there are images on a webpage they’re there to support the content and not excessive, so you don’t have to worry about loading times. What if you need to have an undefined number though, and they happen to be high resolution like a gallery?

For the last site I worked on, the client wanted a gallery of 400×400 pixel images to cycle in a gallery on the front page of the site. They’re about 70KB each so if you’re loading 10 or more of them in the middle of the page, they’ll slow the page load down until they’ve all totally loaded.

The solution I used was to add the images to the DOM after the rest of the content loads.

Read more »