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<LineBreak/> multiple<LineBreak/> lines</TextBlock>
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.
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.
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.
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:
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!
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.
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.
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:
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 http://www.gui-design.ppedv.de/ or just click on the image below to join it.