tags

WPF

[WPF] Markup extensions and templates

Note : This post follows the one about a a markup extension that can update its target, and reuses the same code. You may have noticed that using a custom markup extension in a template sometimes lead to unexpected results… In this post I'll explain what the problem is, and how to create a markup extensions that behaves correctly in a template. The problem Let's take the example from the previous post : a markup extension which gives the state of network connectivity, and updates its target when the network is connected or disconnected :

[WPF] Automatically sort a GridView (continued)

A few months ago, I wrote a post where I explained how to automatically sort a GridView when a column header is clicked. I had mentioned a possible improvement : add a sort glyph in the column header to show which column is sorted. In today's post, I present a new version of the GridViewSort class, which displays the sort glyph. To achieve this result, I used an Adorner : this is a component which allows to draw over existing UI elements, on an independant rendering layer.

[WPF] A markup extension that can update its target

If you have read my previous posts on the topic, you know I'm a big fan of custom markup extensions… However, they have a limitation that can be quite annoying : they are only evaluated once. Yet it would be useful to be able to evaluate them again to update the target property, like a binding… It could be useful in various cases, for instance : if the value of the markup extension can change in response to an event

[WPF] Binding to an asynchronous collection

As you may have noticed, it is not possible to modify the contents of an ObservableCollection on a separate thread if a view is bound to this collection : the CollectionView raises a NotSupportedException : This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread To illustrate this, let's take a simple example : a ListBox bound to a collection of strings in the ViewModel :

[WPF] Automatically sort a GridView when a column header is clicked

It's quite simple, in WPF, to present data in a grid, thanks to the GridView class. If you want to sort it, however, it gets a little harder… With the DataGridView in Windows Forms, it was “automagic” : when the user clicked a column header, the grid was automatically sorted. To achieve the same behavior in WPF, you need to get your hands dirty… The method recommended by Microsoft is described in this article ; it is based on the Click event of the GridViewColumnHeader class.

[WPF] Using InputBindings with the MVVM pattern

If you develop WPF applications according to the Model-View-ViewModel pattern, you may have faced this issue : in XAML, how to bind a key or mouse gesture to a ViewModel command ? The obvious and intuitive approach would be this one : <UserControl.InputBindings> <KeyBinding Modifiers="Control" Key="E" Command="{Binding EditCommand}"/> </UserControl.InputBindings> Unfortunately, this code doesn't work, for two reasons : The Command property is not a dependency property, so you cannot assign it through binding InputBindings are not part of the logical or visual tree of the control, so they don't inherit the DataContext A solution would be to create the InputBindings in the code-behind, but in the MVVM pattern we usually prefer to avoid this… I spent a long time looking for alternative solutions to do this in XAML, but most of them are quite complex and unintuitive.

[WPF] Article about the Model-View-ViewModel design pattern, by Josh Smith

Soon after the release of WPF, people have been talking more and more about “Model-View-ViewModel” (MVVM). This expression refers to a design pattern, drawing its inspiration from the Model-View-Controller (MVC) and Presentation Model (PM) patterns, and created specifically to take advantage of WPF features. This patterns enables an excellent decoupling between data, behavior and presentation, which makes the code easier to understand and maintain, and improves the collaboration between developers and designers.

Build an RSS reader in 5 minutes

Today, I stumbled upon a very handy class : SyndicationFeed. This class, introduced in .NET 3.5, allows to manipulate syndication feeds (like RSS 2.0 or Atom 1.0) with very little code. It can be used to create and publish our own feeds, or to read existing ones. For instance, here's how to retrieve the news feed from Google News and display its title, its hyperlink, and the titles of it's items :

[WPF] Paste an image from the clipboard (bug in Clipboard.GetImage)

Oops… 2 months already since my previous (and first) post… I really have to get on a more regular schedule ;) If you've ever tried to use the Clipboard.GetImage method in WPF, you probably had an unpleasant surprise… In fact, this method returns an InteropBitmap which, in some cases (most cases actually), can't be displayed in an Image control : no exception is thrown, the image size is correct, but the image either appears empty or unrecognizable.

[WPF] Binding to application settings using a markup extension

Hi, this is my first post on this blog, I hope you will enjoy it ;-). If you want to know a few things about me, please check out this page. The end-user of any application expects that his preferences (window size, state of this or that option…) are saved to be restored at the next run : that's why .NET 2.0 introduced application settings as a unified way to persist these settings.