Category Archives: .NET

.NET Framework

Render just the first line of a TextBlock

Sometimes you want a TextBlock to just display one line. For example if you have a huge virtualized ListView that contains many data items and in the DataTemplate you’re using a TextBlock. When the TextBlock has different height, the virtualization leads to some not so nice behavior when scrolling:
The Thumb of the ScrollBar changes its size while scrolling, as the final extend size is not known, as the items in the ListView have different sizes because of multiple lines.

So when you have virtualization, you might want to display your multiline messages in a master-detail-way. Select a single-line-item in the ListView and display the multiline message somewhere else.

But now the problem is, how to tell the TextBlock to render just the first line? Note that I want to adjust the rendering. I don’t want to adjust the strings with a converter or whatever. Just the rendering! :-)

The TextBox has for the single-line-scenario a MaxLines-Property. But the TextBlock does not have this property.
The TextBlock also does not allow to override its MeasureOverride-method to measure the line height and do custom stuff, as this method is marked as sealed in the TextBlock class.

So how could you display only the first line of a TextBlock like this:

<TextBlock TextWrapping="NoWrap">
  Text with

To render just the first line, I use a little trick. I place the TextBlock from above together with a hidden TextBlock with just one line in a Grid. Then I can bind the real TextBlock’s Height to the ActualHeight of the hidden one-line-TextBlock. Here the XAML-code, works like a charm.

  <TextBlock x:Name="txtOneLineDummy" 
             Text="One line" 
  <TextBlock TextWrapping="NoWrap" 
             Height="{Binding ElementName=txtOneLineDummy,Path=ActualHeight}">
    Text with

Note that you could place the hidden one-liner-TextBlock also somewhere else, especially if you have thousands of records that would use a TextBlock via a DataTemplate

Creating a background application with WPF

Sometimes you need to have an application running in the background. Then you don’t want your MainWindow to be visible all the time. Instead you just want to have an icon in the notification area that allows the user to open up the MainWindow and to exit the application. In WPF you can do this easily with the help of Windows Forms’ NotifyIcon-class.

Just add references to System.Windows.Forms and System.Drawing to your WPF-project. Add also an icon-file (.ico) to your project resources. To do this just open the Resources.resx-file in the Properties-folder of your project. Select the Icon-resource and click the Add Resource button. I’ve named it MyIcon.


Then go to the App.xaml.cs and implement it like below. The trick is to never close the MainWindow, as a closed Window cannot be shown again. Instead cancel the closing and just hide it.

using System.ComponentModel;
using System.Windows;
namespace BackgroundApplication
    public partial class App : Application
        private System.Windows.Forms.NotifyIcon _notifyIcon;
        private bool _isExit;
        protected override void OnStartup(StartupEventArgs e)
            MainWindow = new MainWindow();
            MainWindow.Closing += MainWindow_Closing;
            _notifyIcon = new System.Windows.Forms.NotifyIcon();
            _notifyIcon.DoubleClick += (s, args) => ShowMainWindow();
            _notifyIcon.Icon = BackgroundApplication.Properties.Resources.MyIcon;
            _notifyIcon.Visible = true;
        private void CreateContextMenu()
            _notifyIcon.ContextMenuStrip =
              new System.Windows.Forms.ContextMenuStrip();
            _notifyIcon.ContextMenuStrip.Items.Add("MainWindow...").Click += (s, e) => ShowMainWindow();
            _notifyIcon.ContextMenuStrip.Items.Add("Exit").Click += (s, e) => ExitApplication();
        private void ExitApplication()
            _isExit = true;
            _notifyIcon = null;
        private void ShowMainWindow()
            if (MainWindow.IsVisible)
                if (MainWindow.WindowState == WindowState.Minimized)
                    MainWindow.WindowState = WindowState.Normal;
        private void MainWindow_Closing(object sender, CancelEventArgs e)
            if (!_isExit)
                e.Cancel = true;
                MainWindow.Hide(); // A hidden window can be shown again, a closed one not

Now go in addition to the App.xaml and remove the Startup-Uri, so that when you start the application, only the NotifyIcon is added to the notification area

<Application x:Class="BackgroundApplication.App"


Download the sample-project here: Background application

The future of Windows Presentation Foundation (WPF) and what’s coming next

In 2006 I started working with the first preview versions of the Windows Presentation Foundation with my Trivadis fellow Christoph Pletz. We wrote 2007 the very first German article about MVVM and 2008 I published my first book with the title “Windows Presentation Foundation – the ultimate handbook”.

Since then the consultants of Trivadis and I did a ton of big WPF-projects for several customers. During the last year with the hype around AngularJS and HTML5 many customers asked about the future of WPF. Today I’m happy to announce that Microsoft respective the WPF Team shared their roadmap about that bright future. Find it here:

I’m very happy to see that Microsoft invested quite a lot of effort into performance-optimization for the next version. Also the Visual Diagnostics tools in the article with the Live Visual Tree and Live Property Explorer look quite promising. Below a screenshot taken from the article above:


Even Blend has been completely redesigned and supports now differnt things like for example Solution Folders and better Source Control support.

With that next release of WPF I’ll for sure update my book about WPF and bring out a fourth edition next year.


Microsoft TechDays 2013 Basel

Thanks to all who joined my sessions at TechDays in Basel. Also a big thanks to Microsoft Switzerland for organizing this great and successful event. I really enjoyed the two days of networking with awesome people.

Please find the slides and demos of my talks on my homepage by selecting the talk:

If you were not able to attend TechDays in Basel, you can just watch my talks here (they’re in German. Find other talks on on channel9):


Feel free to ask any questions about the topics by commenting here or just send me an email.


Received Microsoft MVP award 2013

Wow, on 1st of July I’ve received my very first Microsoft MVP (Most Valuable Professional) award. I received it in the area “Client Development”.


Thanks to all the people out there in the .NET/C#/XAML-community, thanks to all the readers of my books, articles and blog-posts, thanks to the participants of my public talks, thanks to the colleagues at Trivadis and thanks to Microsoft Switzerland for their great support. In the end I want to thank my family and all the friends and people supporting me in any technical and non-technical way. You’re awesome!

More about the MVP award you can read on

My personal MVP profile is here:

Keep on codin’

Dragging Elements in Windows Store Apps

In Windows Store Apps there are three kinds of Touch-Events:

  • Gesture Events – those are high-level-events like Tapped or RightTapped.
  • Pointer-Events – low-level-events like PointerPressed, PointerMoved, PointerReleased.
  • Manipulation-Events – low-level-events for multi-touch actions like rotate, scale or transform an element.

Pointer-Events consolidate Touch-, Pen- and Mouse-Input. So on the UIElement-class from WinRT you wont find Mouse-Events known from WPF/Silverlight like MouseLeftButtonDown.

In my upcoming Windows Store Apps I want to show how Elements can be dragged/moved with Pointer-Events. You can download the code of the sample here:

MovingElements, or in German ElementeVerschieben-Sample

The app looks like below. By touching an empty space in the yellow area, an Image is inserted. The image can be grabbed with a finger or with the mouse-cursor and moved around. The app works also with multiple fingers. You can grab multiple images at once and move them around.


If you´ve feedback to the code, feel free to comment. Smiley 

As I´m really busy the next weeks/months with the writing of the Windows Store Apps-book, I´m not able to give you big support on the code. Use it or not.


Windows Store-apps with XAML and C# – blog series

Since yesterday evening Winodws 8 RTM is out for developers. This blogpost is the start of a blog-series about developing Windows Store-apps with XAML and C#. The series consists of some informational and some “how-to” posts:

More topics will come. If you’ve topics not listed above you want to read about, write a comment on this post.

The first post about XAML will be written till saturday evening


TechDays 2011 – Demos online

Thanks to all who attended in my session at TechDays 2011 in Basel. My Live-Demo is available via the link below.

TechDays Basel – LiveDemo – Download here

The .zip-File contains a .bak-File that you can restore via SQL Server Management Studio by chosing “Restore Database” from the contextmenu when right-clicking on “databases” in Object Explorer.

Speaking at GUI & Design Conference in Nürnberg

It has been quiet here for some weeks. After I had finished my Silverlight book I just enjoyed the unfamiliar free time I had on Saturday and Sunday. I just used it for hanging around with my family and friends. Now I’m back in the optimum of work-life-balance. I’m going to start a series of blogposts about building apps with Silverlight for the Web and for Windows Phone 7 in the mid of december. So I hope you’re looking forward to that.

With this post I want to inform you about the GUI&Design-Conference. The conference about GUI and Design has a lot of experts and great sessions to offer. I think it’s a special and great conference that you shouldn’t miss as a UI-Developer or –Designer. It would be great to see you there.

I’ve two sessions. One about Pixelshader-Effects in WPF and Silverlight and one about developing and designing Custom Controls in WPF and Silverlight. Find the conference-website on or just click on the image below to join it.


Looking forward to see you there.