Wednesday, October 07, 2009

WPF ComboBox select item when using ItemsSource

I have a WPF ComboBox that is databound to a Linq query.

var mylinqdata = from d in DataTable0.AsEnumerable()

select new


Status = d.Field<string>(0).Trim(),

Progress = d.Field<string>(1).Trim()



dlg.cb_Status.ItemsSource = mylinqdata;

When data is bound to a WPF control using ItemsSource you must change the selected item using SelectedIndex. Here is the code I use.

dlg.cb_Status.SelectedIndex = mylinqdata.ToList().FindIndex(d => d.Status == loom.Status);

Here is the XAML of the ComboBox

<ComboBox Name="cb_Status">
<TextBlock Text="{Binding Path=Progress}"></TextBlock>

Friday, October 02, 2009

Linq group by multiple columns

Here is a bit of linq to group by multiple columns. It’s not very well documented so I’ve included a sample below.The trick is to use an anonymous type after the group by clause.

var data3 = from d in db.Assembly_V2s
group d by new { Column1 = d.AssemblyID / 100, Column2 = d.Description.Trim() } into g
orderby g.Key.Column1
select new { Column1 = g.Key.Column1, Column2 = g.Key.Column2 };

Wednesday, September 23, 2009

HyperLinkField in GridView with multiple fields does not work

<asp:GridView ID="GV" runat="server" AutoGenerateColumns="False">


<asp:HyperLinkField DataNavigateUrlFields="CAM,Floor,Riser" DataNavigateUrlFormatString="HorzHandover.aspx?CAM={0}&amp;Floor={1}&amp;Riser={2}"     Text="Handover"/>     




    <asp:HyperLink ID="HyperLink1" runat="server"  NavigateUrl='<%# "Export.aspx?CAM="+Eval("CAM")+"&Floor="+Eval("Floor")+"&Riser="+Eval("Riser") %>' Text="Signoff"></asp:HyperLink>            </ItemTemplate>




I think this is a bug in HyperLinkField in GridViews with multiple fields do not seem to work at all. You have to turn column into a template to make it work (as shown above)

Thursday, August 27, 2009

Serving Silverlight 3 apps from IIS6

If you want to serve Silverlight 3 apps from an IIS6 server running on Windows Server 2003 you need to add the following three mime types (I added them at the server level). If you don’t make these changes the web page will show a blank Silverlight window. You can right click to get the Silverlight menu but your app does not run.

.xap     application/x-silverlight-app
.xaml    application/xaml+xml
.xbap    application/x-ms-xbap

I discovered that IIS needs to be restarted before the settings take effect. To restart IIS run IISRESET from a command prompt.

Saturday, August 22, 2009

WPF auto fading error message

Here is a small control template to display an error message. The message will fade away after a couple of seconds. You set the message like this:-

Label1.Content = DateTime.Now.ToString();

Here is the xaml:-

<Window x:Class="TRM_Uploader.Window2"
Title="Window2" Height="300" Width="300">
<ControlTemplate x:Key="MyErrorMessage"
TargetType="{x:Type Label}">
<StackPanel Orientation="Horizontal"
<Border CornerRadius="4"
<Label Foreground="White"
<!-- This TextBlock accepts the content from the parent 'label'. It is not actually displayed, just used to bind data-->
<TextBlock Name="Part3"
Text="{TemplateBinding Content}"
<!-- This TextBlock binds to the one above and triggers an animation when it gets updated. -->
<!-- Its required because TemplateBinding does not allow NotifyOnTargetUpdate -->
<TextBlock Margin="2"
Text="{Binding ElementName=Part3, Path=Text,NotifyOnTargetUpdated=True}"
<!-- When Text property of Part2 is blank hide entire control-->
<Trigger SourceName="Part3"
<Setter TargetName="ErrorMessage"
<!-- This trigger is called when Part2 updates which in turn is updated by Part3's databind.
It uses animation to make the label fade away after a few seconds. -->
<EventTrigger SourceName="Part2"
<Storyboard TargetName="ErrorMessage">
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To="1.0" />
<DoubleAnimation Storyboard.TargetProperty="Opacity"
BeginTime="0:0:1" />
<StackPanel Orientation="Vertical">
<Label Template="{StaticResource MyErrorMessage}"
<Button Click="button1_Click">click to test</Button>

And the c#


public partial class Window2 : Window
public Window2 ()

private void button1_Click (object sender, RoutedEventArgs e)
Label1.Content = DateTime.Now.ToString();

Thursday, August 20, 2009

How to make a bootable USB memory stick in Windows 7

I spent ages on this last night without any success, tonight it took me about 60 seconds. Here is the trick.

First you need to download Windows Enabler from This allows any disabled controls to be re-enabled. Install the program and click on its system tray icon to enable it. The Icon should show ‘on’ to show it’s working :

With Windows Enabler running you can go into Windows Explorer and format your memory stick (obviously this will erase everything on the stick). Normally the “create an MS-DOS startup disk” is disabled for everything except floppy disks but with Windows Enabler running you can click the disabled checkbox to use the option with any drive.

I think the file system needs to be set to “FAT”. Once formatted your stick will be a bootable MS-DOS 4.9.3000 boot disk. Easy!

IE image visibility bug

I found two very strange bugs in IE 8 yesterday (probably in other IE versions as well). I have an image and some text in a SPAN that is made visible with some JavaScript. Here is the HTML :

<img id="ctl00_ContentPlaceHolder1_Image3" src="Graphics/waiting.gif" align="middle" style="border-width:0px;visibility:hidden" />

<span id="ctl00_ContentPlaceHolder1_Label4" style="visibility:hidden">Please wait. Adding results to database.</span>

The Javascript attempts to change the visibility like this :'visible';'visible';

for SPAN tags this works. for IMG tags it has absolutely no effect!

The solution is a horrible hack :-

<script type="text/javascript">

function UploadComplete()



window.setTimeout(TurnAnimatedGifBackOn, 500);


function TurnAnimatedGifBackOn ()


var tmp = document.all.ctl00_ContentPlaceHolder1_Image3;

if(typeof (tmp) == 'object')

{ = 'visible'; tmp.src='waiting.gif';




To fix the problem the IMG tag has to have it’s display property toggled from ‘none’ and back to ‘block’.

The second bug is that animated gifs stop animating when a page post back occurs. To fix this problem I’ve used a timer to alter the image 500ms after the page post back. To get the animation working the src tag must be reloaded with the image url.

Other browsers do not seem to have this problem.

Enable Hardware Virtualization on Sony VGN-AR71ZU

It is really annoying to discover that Sony disable hardware virtualization and do not provide a bios setting to allow it to be re-enabled. This prevents you from being able to use Windows Virtual PC/MP Mode in Windows 7. I finally managed to hack the bios and now have full hardware virtualization!

Hacking the bios is actually trivial provided you know the right register to change. Chris Hay has a blog post which describes the change for a VGN-AR71ZU (bios = Phoenix R2090J8). (

The trouble I had was creating a bootable USB memory stick. I spent hours messing around last night with absolutely no success. In the end I gave up on the USB stick and found an old USB floppy drive. I downloaded the ms-dos 6.22 disk images from MSDN and made myself a set of three setup disks. I then booted off the floppy, exited ms-dos setup with F3 and ran SYMCMOS as described in Chris’s blog post. The process was not helped by the fact that the USB floppy would not work on Vista or Windows 7 so I had to create the ms-dos setup disks on an old XP machine.

Wednesday, July 01, 2009

Firefox 3.5 causes WiFi problems

I have a Intel 4965AGN WiFi card running Windows 7 RC. Yesterday Firefox 3.5 was installed. I think the new location services (which uses WiFi to work out your location) is causing the Windows 7 WiFi to stop working. uninstalled Firefox and WiFi started working again.

Friday, June 19, 2009

FTP.exe on Windows 7

I have a scheduled batch file that sends some files to an FTP server. This stopped working when I upgraded to Windows 7 a few weeks ago. I finally tracked down the problem to the new improved firewall in Windows 7. If you want to use FTP you must edit the Inbound Rule for "File Transfer Program" Private profile and change the TCP action from blocked to allow.

Thursday, May 14, 2009

Tweeting real time energy usage

I finally joined Twitter and promptly wrote a cool windows service that tweets the cost of electricity used in our buildings every half hour. See Could this be the most boring tweet of all time?