Code
Here are links to a 3 part series with code that let you use C#-like lambda expressions in XAML instead of writing value converters.
For example, instead of writing a value converter that takes a DateTime and calls ToShortTimeString you can write:
<TextBlock Text='{Binding Source={x:Static s:DateTime.Now},
Converter={fix:Lambda "dt=>dt.ToShortTimeString()"}}'>
WPFix Part 1 (Lambda Converter Extension)
WPFix Part 2 (Binding Extension)
WPFix Part 3 (Extension Methods)
This post on the WPF SDK blog contains almost everything you need to know to change your window appearance using WPF, this includes:
Extending Vista glass effects into your window.
Maximizing a window with WindowStyle of None without covering the taskbar.
Removing information from the application title bar (Vista only).
Drawing in the window title area in a way that is compatible with Vista glass effect.
A must read for every WPF developer.
This is the fourth (and last) part in a series about how to get the latest look and feel for your WPF application, the previous parts are:
Why am I Getting Old Style File Dialogs and Message Boxes with WPF
Will Setting a Manifest Solve My WPF Message Box Style Problems?
The Application Manifest Needed for XP and Vista Style File Dialogs and Message Boxes with WPF
In part 3 of this series we’ve set a manifest file and almost solved our problem – we finally got rid of...
This is the third post in a series about how to get the latest look and feel for your WPF application, the first part is here and the second is here.
Here is the manifest file needed to get the newer version of the common controls library, just setting this manifest will get us the newer (XP or Vista) look for message boxes and the XP look for the file open and close dialogs – but when running on Vista this will not give us the Vista style open and close dialogs, I’ll show how to fix this in the next...
This is the second post in a series about how to get the latest look and feel for the message boxes and common dialogs in your WPF application, In the first post I described the problem.
I just understood from a reply I got that I wasn’t clear enough about all the little details in the previous post, I hope this will clarify everything a bit:
This approach works, I know because I tested it myself, it works on XP SP2 and on Vista RTM, I didn’t test it on a 64bit Windows yet but I hope to...
This is the first post in a series about how to get the latest look and feel for the message boxes and common dialogs in your WPF application.
You wrote your WPF based software, your user interface looks great but the moment you display a message box or a file open/save dialog what pops up is a Windows 2000 style dialog.
For example, I’ve created a WPF application with two buttons on the main window, as you can see from the image below the application picked up the XP style with the rounded buttons and everything.
Let’s click the “Message Box” button, the...
If you use WPF’s data binding with validation a red border appears around controls with invalid values, you can also use the Validation.ErrorTemplate attached property to get a better look for the invalid controls.
But – if you try this you’ll discover that there’s a new tab stop on you from, the red frame (or your own template) now accepts focus, breaking tab navigation.
The problem is that that the validation framework will add a control to the adorner layer in order to display the error indicator – and this control is focusable.
To fix it just add the following to the Window’s...
Creating good software is a very complicated, even in a small project there are hundreds if not thousands of little details you have to get right and it's easy, not to say tempting, to reflect this complexity in the user interface.
High end "enterprise" software is actually expected to look and feel very complex, otherwise how can you justify the amount of money you spent on it.
The concept of simplicity is very popular right now in the software development world, and someone who hasn't worked on "simple" software before might think simplicity is about doing less, or making the code simpler,...
I needed a way to test if a background color is light or dark in order to choose an appropriate text color (black on light colors and white on dark colors), you can find yourself in the same problem if you try to convert an image to grayscale.
I found many approaches that didn’t work well, I’ll describe them and the problems I discovered below – but first – the successful solution.
I finally found a solution that actually works on this web page the formula is:
brightness = sqrt( .241 R2 + .691 G2 + .068 B2 )
Or, in C# using the...
WPF’s styling and templating support is amazing, but some controls are easier to template then others, one of the least intuitive to style controls is the ListBox, it’s very difficult to change the ListBoxItem selection background color for example.
Here is a post on the WPF SDK blog that explains how to do it.
I’ve posted this here so I’ll know where to look the next time I want to style a list box
One of the more popular eye-candy effects is reflection, and WPF can do really good reflections, this post on Xamlog shows a really nice technique to add reflection to just about anything.
When I was showing this technique to a co-worker he asked me if it can be encapsulated in a control, well, why not?
So I quickly wrote this control, the control is a "Decorator" like a Border, that means you can add one child to it and it draws itself around that child (obviously this child can be a panel with multiple children).
This control is divided into two halves...
This is the final part of the WPF image viewer tutorial: Introduction, Part 1, Part2, Part 3, Part 4, Part 5 and Part 6.
So, what are transforms?
This is an extremely short and simplified explanation, like most of the topics I cover in this tutorial you can use this to get started but if you want in-depth information I suggest you but this book.
Transforms are a way to modify how something is rendered to the screen, you can take any control and use transforms to move it, scale it, stretch it, rotate it and more.
There are two ways to apply a transform in WPF, Render...
I have a problem with printing in WPF, it happens consistently on my main development machine but I haven’t been able to reproduce it anywhere else.
And I have a solution.
This is a part of the WPF image viewer tutorial: Introduction, Part 1, Part2, Part 3, Part 4 and Part 5.
In this post were going to replace the big blue area in the center of our window with an image, obviously the image we want is the selected thumbnail from the list box to the left.
First let's replace the blue rectangle with an image control, then we'll figure out how to get the image into it, just replace the second rectangle in the XAML file with an <Image/> element.
Now let's play with data binding, WPF has very nice support for binding...
This is a part of the WPF image viewer tutorial: Introduction, Part 1, Part2, Part 3 and Part 4.
In this part where going to organize our application layout, currently our entire application window just contains a list box full of images, it was easier to do then with WinForms but that doesn't make it very useful.
The WPF controls that can contain multiple children and arrange them are called panels, WPF has several useful panels we can use, in this post we'll use the StackPanel and DockPanel panels.
StackPanel
The StackPanel is probably the simplest panel in WPF, it just arranges all its children...
This is a part of the WPF image viewer tutorial: Introduction, Part 1, Part2 and Part 3.
I promised something nice and we got all the way to the 4th post with a program that displays a list of filenames, it's time to start seeing some images.
The WPF list box control isn't the old Windows list box, while the old list box could only display a list of strings the WPF list box can display any list, if we tried to display buttons we would have got a list of completely functional buttons you can press – but we used a list...
This is a part of the WPF image viewer tutorial: Introduction, Part 1, Part2.
In this part we'll connect the C# code with the XAML UI, just open Window1.xaml, find the Grid element and add the following line between <Grid> and </Grid>
<ListBox ItemsSource="{Binding AllImages}"/>
This will create a list box and bind it's ItemsSource property to the AllImages property we wrote in the last part, it you'll run this you will get a window with a list box covering the entire window and the list box showing the names of all the image files.
The reason we set the DataContext in the...
This is a part of the WPF image viewer tutorial: Introduction, Part 1.
This part of the tutorial has very little to do with Wpf, we just have to define and fill the data structure used to hold the image information.
First let's define a class to hold an image and it's file name, I've defined this class as a nested class of Window1 because it doesn't matter where we put it.
public class MyImage
{
private ImageSource _image;
private string _name;
public MyImage(ImageSource image, string name)
{
_image = image;
_name = name;
}
public override string ToString()
{
return _name;
}
public ImageSource Image
{
get { return _image; }
}
public string Name
{
get { return _name;...
This is part of the WPF image viewer tutorial series, you can read the introduction here.
Create the project
We'll start writing code soon, but first a short overview of what visual studio does for us.
After you installed all the required software you can start Visual Studio
Select File -> New -> Project From the main menu.
In the project types tree (on the left) select "Visual C#" and on the templates list (on the right) select "Windows Application (WPF)".
Enter "ImageViewerTutorial" into the name box at the bottom and click Ok
Visual Studio should now create a skeleton WPF application for you.
Visual Studio created 2...
I just love WPF, it's the best UI system I've ever worked with.
When I want to introduce my friend to WPF I just build a small image viewer with them, it takes me less than 30 minutes and it really shows the power of WPF features like templates and data binding.
I've decided to expand my little demo and post it to my blog, this will take several posts – each one showing another nice feature.
Before we begin there are a few things we need in order to write WPF code:
Windows XP SP2 or Windows Vista –...
Finally, the last post in the auto-update series. Previous posts: part 1, 2, 3, 4, 5, 6 and 7
For 8 posts I've been writing about the technical details of building a good automatic update component from scratch, what I haven't wrote is that it's probably silly to write your own instead of using something that already works.
ClickOnce
ClickOnce is built into .net (version 2.0 and later) and is extremely easy to use, all you have to install your software using ClickOnce. You prepare the ClickOnce package – just a few files on a web server (all your program files and...
This is part 7 of a series, previous posts: part 1, 2, 3, 4, 5 and 6
In the previous part I promised to talk about how to update a program while it's running, if you were hoping for some cool file system locking trick you are going to be disappointed.
The method I used for yaTimer, the time tracking application I've developed, is very simple. All the program files are not stored directly in the program folder but in a sub-folder this sub-folder is different for each version, so what you get is:
Program Files
yaTimer
1.1.0 all the files for version 1.1.0
And...
When working on yaTImer's new report engine I got myself into a bit of a problem, I'm blogging about it because I couldn't find an answer on the web and I can't believe I'm the only one with this problem, so I hope someone will find my solution helpful (or maybe suggest a better one).
My reports engine generates a FixedDocument that I can print or show to the user, because the report can contain a lot of information generating the document can potentially take some time, so I went for the easy solution and dropped a BackgroundWorker into the code.
So...
This is part 6 of a series, previous posts: part 1, 2, 3, 4 and 5
In the end of the previous post in the series I wrote this part will be about updating the program while it's running and updating the service itself, this will wait until the next post because there is a small limitation to our auto-update service I didn't talk about – you can't interact with the user from a Windows service.
Showing user interface from a service has been "not recommended" for a long time and disabled by default – but it takes just one checkbox to...
This is part 5 of a series, previous posts: part 1, 2, 3 and 4
In the previous part we finally got our auto-update system working, the only problem is that it only works if we have administrator rights, I find this totally unacceptable (it also doesn't work at all under vista).
The standard solutions is relatively simple, have a second component on the user's system that runs with sufficient privileges and let it do the actual updating, another approach is to prompt the user for the administrator name and password and use them to run a setup program with administrator rights,...
This post may be news to developers in the US but I (like most of the people in the world) have lived all my life on the wrong side of incorrect internationalization/localization efforts – so you can trust me on this.
This post is written from a .net perspective, but it can be easily translated to any other environment.
And the big tip is: respect the user preference, remember it may be different (or, in technical terms CurrentCulture, InvariantCulture).
Respect the user preference - every string that is displayed to the user or entered by the user should be...
This is part 4 of a series, previous posts: part 1, part 2 and part 3.
After we got the download details in the previous post it's time to download the update.
yaTimer - The software I'm selling, and obviously the easiest way to track your time - is a small application with small updates, so it just uses the .net WebClient class to download the file from the server, it's simple and easy to write the code and we can move on.
On the other hand, if your application needs to download large files of the web you will soon discover the...
This is part 3 of a series, previous posts: part 1 ,part 2.
In part 1 I presented a quick hack of an auto-update feature, In this part I'll describe a real auto-update component, and one that can be implemented very quickly.
The first thing our auto-update component has to do is detect when an update is available, since we don't have much time to implement this feature (we have a lot of other more visible features we want to finish for this version of the software), we will take the easiest option possible – store a small file on the web...
This is part 2 of the series, part 1 is here.
Before describing how an auto-update feature might work I wanted to talk a bit about security, an auto update feature by definition downloads and runs program from the internet, your users trust you that your auto-update mechanism will only download and install updates to your software – don't abuse this trust.
The internet is a dangerous place, the bad guys might find a way to modify your web site – or to completely hijack it, when your program downloads updates it has to be 100% sure those updates are from you...
Here is the C# source code for the previous blog post How To Write an Automatic Update System Part 1 – the Simplest Auto Update Ever this code is for .net version 2.0 or 3.0
try
{
System.Diagnostics.Process.Start("http://www.example.com/upgrade_from_version_1.htm");
}
catch
{
// ignore exception, might be FireFox’s fault
}
Just let the system do the heavy lifting, it will automatically find the default browser for you and load the web page.
The try-catch is needed, some versions of FireFox will not handle this correctly and cause an exception (as well as an error message) – but it will still load the web page after throwing the exception.
Note that...
You are working on finishing the first version of your software product, you want to release it as soon as possible, you already decided to delay some features to the next version and you don't have the time to write an automatic update feature or to buy and integrate an existing solution. What do you do?
You just add a page to your web site, the page just says there is no update available, in your application the "check for updates" button or menu item just opens that page in the user's default browser. When a new version is available -...
|