WPF Label vs. TextBlock

Background

For a client, I am currently writing a big WPF application. All the clients are still using Windows XP, but since the users seem to like the application more when Aero is being used, we decided to use the Aero theme that comes with WPF.

By default, the Aero theme is great, but there are some things I don’t like about the themes. Actually, none of the themes support what I need, and that is a default margin for all controls. For example, if you create a Grid and place some text elements (Label or TextBlock) and a TextBox, you want automatic spacing, right? Or are you one of the hundreds that set the margins for all controls manually or define them in the grid?

A lot of blogs on the internet already handle the discussion Label vs. TextBlock, and all I read have this bottom line: “TextBlock is lighter, so use it if you don’t need the Accelerator feature of the Label control”.

I don’t agree with this conclusion, because Label is used to display text as a label. TextBlocks are used in a lot of controls to display text. So, if you use TextBlock as a replacement for the Label control, you will see this result when you increase the margin of the text displayed before TextBoxes:

textblock_vs_label_using_textblock

Figure 1 - Using TextBlock

textblock_vs_label_using_label

Figure 2 - Using Label

As you can see, figure 2 does what it should do (according to the design). The labels have an automatic margin so all controls and spacing is done automatically by the parent control (in this case a Grid inside a Tab Control).

Conclusion

If you want to use styles in WPF correctly (and you need to modify the margin, etc), I recommend to use a Label instead of a TextBlock. TextBlocks are used inside a lot of controls, and modifying the TextBlock style has a major impact on how most controls (such as a Button, ComboBox, GridView Columns, etc) behave.

So, if you want to display text as a control itself, always use Label. The benefits of showing styles correctly (and have more control over styles and themes) are better than the fact that a TextBlock is lighter (anyone having problems with the performance of WPF regarding Labels, never heard of it).

kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Windows Workflow Foundation

I finally got the opportunity to work with WF (Workflow Foundation) in a real-life situation. For one of my clients, I have implemented an application that supports workflows. Since I knew that the existing workflow system we used was overpriced and did too little for the too much being paid, I recommended to use WF. The software I developed already used providers so it was easy to write a new “workflow provider” for WF (and still be able to use the old system as well).

After some investigation, the client agreed to go ahead with WF, and it all seems to work perfectly now (so a happy me and a happy client). However, below are some things I noticed during the integration of WF.

Workflow and its activities not fully recognized

I wrote a proof of concept for the client so they could see that WF fits their needs. This POC worked perfectly. However, when porting the code to the final software, I was not able to create workflows successfully. When investigating the persistence database, I noticed that only the top-level activity was persisted (and the activity was closed immediately).

After searching for a few hours, I decided the only difference was that the workflow definition was located in a separate assembly in my POC. When I moved the workflow definition to a separate assembly, it all seemed to work perfect (workflow including its activities were fully recognized).

I will add more things I stumbled upon soon.

kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

WebSVN extension

WebSVN is a great way to view Subversion repositories in a web browser. It supports viewing the source code, blaming, log messages and more. It even supports parent listing so you can view multiple repositories in the same directory in one clear overview.

However, as always, I do the things just a little different than others. My subversion repositories are sorted by company and project. An example:

RepoExample

As you can see, the repositories are divided into logical (at least, I find them logical :)) groups. However, this setup required me to host several different versions of WebSVN:

  • RepoExample (root)
  • companies/company1
  • companies/company2
  • companies/sites

This is because the list parent only lists one directory, not all subdirectories.

To solve this problem, I have written a small extension. The extension really works very simply. You select a root directory (in the example case, RepoExample). Then, the extension will search all subdirectories and check whether the specific directory is a Subversion repository (by checking the directory structure).

It lists all the top directories as groups, and then adds all sub repositories into that group. The result of the extension:

websvn_result

As you can see, it adds a group (directory name except the repository name, such as companies/company_1). Then, all repositories are added after the dot (.).

There is just one downside of this extension: the default authentication files per repository are not recognized. In other words, everyone that is allowed to log in via Apache in the root where WebSVN is located (via webdav modules), is able to see the repository index.

To add this extension to your WebSVN installation, simply perform the following steps described in the readme.

WebSVN extension.zip (4.58 kb) [Downloads: 13]

kick it on DotNetKicks.com 

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

WPF Windows Extension Methods

Sometimes, you need some functionality that is not included in the .NET framework. Extension methods are a (some people think bad) way to add functionality without the need to subclass a class (such as Window) and derive all the new classes that need this functionality from your customized class.

For one of my clients, I have developed a add-in based system that is based on the Add-In namespace. The Add-In namespace is a great way to split functionality into separate add-ins that you can load on-demand. However, there are some disadvantages. For example, Windows that you show as a dialog window in an add-in don’t get the right owner. However, you can’t touch the real owner (the add-in host) since you cannot serialize a Window object.

To overcome such problems, I have written some extension methods. Some of them are described below. The source code for these extension methods can be downloaded at the bottom of this post.

WindowExtensions.SetOwnerWindow (3 overloads)

This method sets the owner of a Window. If the Owner property of a the Window object, the Owner will be kept because it is explicitly set by the developer. However, in case that the Owner window is not set, the method tries to find the main window of the process. This way, the Window will be displayed modal to the main window of the process.

FrameWorkElementExtensions.BringWindowToTop

Sometimes, you want to bring a Window to the top of the desktop. Activate will not always work in case you want to force the window to show on top of all windows. But, you don’t want to make the window system modal either, because the user must be able to focus other windows as well. In that case, especially when you are using the Add-In namespace when you can’t get to the main window directly, this method is very helpful.

Update (2009-03-03): I have made some small modifications so that existing owners will be respected.

CatenaLogic.Windows.Presentation.zip (4.12 kb) [Downloads: 35]

kick it on DotNetKicks.com 

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Sort Visual Studio 2008 Projects alphabetically inside Solution Folders

It is a known bug that Visual Studio does not sort projects alphabetically when the projects are located inside Solution Folders. Since I am working on pretty large solutions (more than 20 projects for 1 solution), and I like to order the projects via Solution Folders, this is a very irritating bug.

However, I accidently found the solution to this problem. Follow the steps below to sort your projects alphabetically inside Solution Folders.

  1. Right-click on a project and select rename (or simple select a project, wait 1 second, and click it again or press F2)
  2. Don’t change the name, simply select another project

Et voila, your projects are sorted like Visual Studio should do!

kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5