Posts

WPF Validation Error Disappearing when Switch between visual items

Typical daily trials of a Software Engineer – getting WPF error validation working can be a bit of a pain, it’s a bit buggy.

I hit a problem today whereby errors marked for text boxes on a tab would disappear if you moved away from the tab and then back again, it turned out to be a common WPF problem and this post provided a fix, you just need to wrap your tab content inside an:

element. Bit of a pain but at least it seemed to work once I put these elements in!

Interesting Analysis of the State of Microsoft’s Software Development Tools

The Register published an interesting article on the state of Microsoft’s software development tools today – it focuses on tools for developing desktop apps. Although well written, the article doesn’t make up-beat reading and is in fact quite depressing. WPF and related development tools have been let languish for so long now it is hard to see if it can be brought up to scratch in any reasonable time-frame….

I suppose the message is that things have moved on and that most apps that are consumed on the desktop, except in a few niche areas (machine vision being one of them?), will be delivered on the web via our new friends HTML5, javascript, angular.js et. al.

What do you think? Does Microsoft have the ability or the will to pull out of this nose dive? I hope they do as they have been leaders in this area before and it is a shame to see things decay….

Crystal Reports in WPF, which references do I need to add to my project?

In this software development game it can be hard to avoid developing reports, I haven’t had the need to use CrystalReports in many along year, but last year I had the misfortune of using Microsoft’s shoddy reporting services for a client and decided after that that I would try at all to avoid a repeat of the experience!

So here I am with SAP’s shiny Crystal Reports, so far so good, except for some slightly worrying bitness problems, I am developing on Windows 7 64bit targeting x86 and found that I _sometimes_ had to set my project’s build target to x86 (from AnyCPU) to avoid getting exceptions thrown like this:

With an inner exception like this:

Anyway back to the point of the post:

Which references need to be included in my WPF project?

There are millions of them to choose from and I hate having to add them all if I know they are not all required, so here’s what I found that you need at a minimum:

WPF / XAML: Binding ComboBox directly to enum values

This is a note to remind me how to bind a xaml combo box directly to an enum property defined in the ViewModel. This is handy if the ComboBox values for display match exactly those defined in the backing enum.

So imaging that we have an enum defined as follows:

Now imagine that we want to include a ComboBox in our view that allows the selection of these enum values and whose SelectedValue property binds with a property of our view model, in xaml terms:

So we need to add a property to our ViewModel called UserTypes that will enumerate our enum’s values so that they can be displayed in the combo box. We also need a property called UserType that will bind to the ComboBox’s selected value like this:

And that should do it…. But knowing WPF and XAML you will probably end up fighting with it for a good few hours! ;-)

Have a WPF application remember its size and position between runs

Sometime software development is all about doing the same things over and over again! I keep on forgetting how to get a windows application to remember it size and position in between runs, and I keep on re-finding this handy post that describes how it’s done:

http://stackoverflow.com/questions/847752/net-wpf-remember-window-size-between-sessions

The first answer always works like a charm – copy and paste city! ;-)

Thanks ChrisF!

Windows WPF – Always scroll to end of ListBox when new item added

If anybody is looking for a quick and dirty way of getting a list box to always scroll to its end then have a look here:

http://stackoverflow.com/questions/10884031/wpf-raise-an-event-when-item-is-added-in-listview

Take a look in Item #6, it worked well for me although I am aware that it may be seen somewhat as a hack – but then sometimes WPF just forces you into these situations as it can make it so damn hard to do the simplest of things!

WPF Application add Console window for stdout etc.

I thought this would be quite hard to achieve, but it turned out to be easy. I wanted my WPF windows app to run with a standard console output so that I could see all tat was written to stdout via printf() etc from some C code that is linked in to the WPF app via a C++/CLI interface. To achieve this it was a matter of setting an application setting as detailed in this post:

http://blog.wpfwonderland.com/2009/03/01/adding-console-window-to-wpf-application/

In summary you just need to change the main App’s ‘Output Type’ setting to ‘Console Application’

Thanks to XAML Wonderland for the post!

.NET WPF – Provide value on ‘System.Windows. Baml2006.TypeConverter MarkupExtension’ threw an exception.

Here’s a strange WPF error we encountered at a client’s site today:

 

[code]Provide value on ‘System.Windows.Baml2006.TypeConverterMarkupExtension’ threw an exception.[/code]

 

If you get this error, then chances are your running your WPF app on Windows XP, and your app will probably work fine on Windows 7. Here is a good post about the problem:

 

http://blog.themagicsoftware.com/2011/07/provide-value-on-system-windows-baml2006-typeconvertermarkupextension-threw-an-exception.html

 

In our case we just had to remove an unneeded Icon attribute from our .xaml Window element and the error went away… I am not quite sure why the error occurred but now it’s gone anyway!

 

WPF Slider Not Working with Touch Screen

We hit a problem yesterday when we were testing one of our Microsoft WPF applications on a touch screen set-up, one of the app’s sliders wasn’t correctly responding to touch events and it couldn’t be dragged properly using the touch screen even though mouse control still worked perfectly.

 

It turns out that there is a bug in the WPF software framework related to touch that needs to be programmed around. The problem was fixed by following the instructions detailed here:

 

http://nui.joshland.org/2010/04/why-wont-wpf-controls-work-with-touch.html

 

Many thanks to JOSHB for posting the workaround!

 

.NET, WPF – BitmapImage File Locking

Q: How can I stop BitmapImage, in .NET Windows Presentation Foundation (WPF) from locking the source image file?

 

A: By default BitmapInfo seems to lock the source image file so that you can use the bitmapinfo object and modify or delete the source file at the same (or similar) time. For example, this C# will probably yield a locked image file:

 


var bitmap = new BitmapImage(new Uri(imageFilePath));
// use bitmap...

 

You can get around this locking problem as follows, it’s a little bit more long-winded but it worked for us….

 


var bitmap = new BitmapImage();
var stream = File.OpenRead(imageFilePath);
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
stream.Close();
stream.Dispose();
// Use bitmap.....