Archives / 2009
  • Centering Text on a WPF Shape

    In a WPF application I am building right now, I had a need to create different sets of shapes and put some text within those shapes. The various shapes I needed were things like rectangles, circles, ellipses and triangles. WPF can create these shapes; however, shapes in WPF are not containers, so you cannot add any text inside of them. This article will show you how to put text into each of these shapes in a couple of different ways.

  • Get RGB Values from WPF Color

    Recently, I have been setting up a lot of WPF styles for an application I am working on. During this project, I needed to set a lot of colors. Instead of hard-coding colors, I use resources. For example, I am using the <DropShadowEffect> on many of my controls. This <DropShadowEffect> element has a “Color” attribute that you can set as shown in the code below:

  • Load Resource Dictionaries at Runtime in WPF

    One of the really cool features of WPF is the ability to style controls. Styles in WPF can be grouped together into Resource Dictionaries. These resource dictionaries can then be loaded dynamically at runtime with just a small amount of Visual Basic or C# code. In Figure 1 you see an example WPF form that allows you to type in a XAML file name that contains a resource dictionary. The styles within this resource dictionary are applied to a Grid control. In this case, it is just a simple gradient fill, but the same concept will apply even if you had styles for all controls in your application.

  • Change Templates Dynamically in WPF

    WPF has the flexibility to modify the look of your controls dynamically at runtime with just a few lines of code. Take a look at Figures 1 and 2 and you will see two different views of the same list box and data. To accomplish this, you simply need to setup two different resources of XAML code that you can switch between at runtime.

  • Creating a Base Window Class in WPF

    Unlike Windows Forms, there is no Visual Inheritance in WPF. Luckily you don’t need visual inheritance as you can use User Controls to get a re-usable UI. There are times, however, when you want a base Window class so you can have common functionality in all your WPF windows. To accomplish this you can create a base class from which all your WPF windows can inherit. To use the base class you will need to make one simple change in both the XAML and in your code-behind class.

  • Create a Login Screen in Silverlight 3

    After my last blog post on “Create a Login Window in WPF”, I had a lot of requests for how to create the same login screen in Silverlight 3. There are actually just a few changes that had to be made to get the same look in feel in Silverlight 3. Figure 1 shows what this login screen looks like under Silverlight.

  • Create a Login Window in WPF

    Most business applications require some sort of security system. You can always use Windows Authentication to authenticate a user, but sometimes you might want your own authentication scheme. When you do, you will need to create a login screen for your user to enter her login id and password. This article will explore creating a login window in WPF.

  • Binding to Config Settings in WPF

    Almost every application has a configuration file used to store global settings. What would be cool is to have a global settings class that reads this data from the .Config file, then can be used to bind to UI elements within your WPF application. This is actually very easy to accomplish using the data-binding features in WPF.

  • Creating Border-less Windows in WPF

    When creating a splash screen or to just have a different look for the windows in your WPF application, you might wish to eliminate the border to give it a custom look (see Figure 1). This can be done very easily with WPF.

  • Using a WPF StackPanel for Business Forms

    In WPF if you want to layout a typical business form like the one shown in Figure 1, most people would use a Grid control with rows and columns. However, you may also use a StackPanel control. While sometimes you might need the re-sizing capabilities of a Grid, you sometimes just need a fixed size. For a fixed-size form a StackPanel control can be a little easier and offers a little more flexibility.

  • Split Name into First and Last

    Not too long ago we received a text file of customer names. The problem with the file is it just had one field "Name". One of the requirements of our database application was to have a first name field and a last name field so our customer could search for their customers on either field. As a result we needed to take this text file and split out the data into the two fields from the one.

  • Date Handling Tip: Get the Month Name

    There are a lot of great date handling methods attached to the DateTime class in .NET. However, if you wish to get information such as the current month name, you will not find it in this class. The reason is because .NET needs to take into account local culture and languages. Thus, methods that return something that is specific to a language or a culture are placed under the System.Globalization namespace. As an example, below is the code you would need to write to return the month name as a string in any language.

  • Check if String is All Lower or All Upper Case

    Funny how sometimes there are so many ways to accomplish the same thing. In a project last week I needed to check if a user entered a sentence in all lower (or could have been upper) case. So I immediately went to the most simple solution; using the ToLower() method on the string object and comparing the original string input to the lower version of the same sentence. The code is shown below.

  • Using The ConnectionStringBuilder class

    Building a connection string from scratch can sometimes be a little daunting when you do not know the exact syntax. Of course, you can always visit and find some great help there. In lieu of this you can also use the ConnectionStringBuilder class. Each of the ADO.NET providers supplies a version of this class that will build a connection string for you. Below is an example of how to use this class.

  • Using Parameters with Dynamic SQL

    Some programming situations require you to use Dynamic SQL. Of course the problem with using Dynamic SQL is that this can lead to SQL Injection attacks. However, you can avoid these problems, by just changing how you submit Dynamic SQL to your back end database.

  • Cloning a DataRow

    I can't even tell you how many times over the last few years I have had to clone a row from one DataTable to another DataTable. To make this easier, I created a method that I can call at anytime to create this new DataRow and return a new DataTable back to me. I have another overload of this method that I can also pass in the new DataTable. In ADO.NET there is no easy way to take a single row from an existing DataTable and copy it to another DataTable. The major reason why it is not so easy is you can not add a DataRow that exists in one DataTable to another DataTable. As a result you must create a new DataRow object and copy all of the values from the original DataRow into this new one. You can then create a new DataTable (or use one with the same structure), and add that DataRow to that new DataTable. Below is a method that you can call to accomplish the copying of a single row from one DataTable to a new DataTable.

  • About Nothing

    Sometimes in your code you will need to check to see if a value is nothing/null or not. In .NET there are many different ways to check for this condition. It can also be different depending on the language you use. These little differences can really bite you in the a**, so you need to be aware of the differences.

  • To Thread or not to Thread, Is That the Question?

    A lot of developers want to use threads to offload some processing. There are many different ways to use threads. More often than not it is not a question of how to do threading, but whether or not you should. If you are trying to solve performance problems with your application, you might not want to employ the use of threading, at least not until you have exhausted all other methods of enhancing performance.

  • Why you Should Move to WPF

    If you have not taken a look at WPF yet, you really should. WPF is a great desktop development platform. Granted all of the of the tools are not yet in place, but Microsoft is pouring millions of dollars into developing WPF tools. Windows Forms is now considered a legacy technology and will no longer be updated. These two reasons alone are enough to convince you that you should start taking a little more than a serious look at WPF.

  • Why Do I Need OOP?

    Most developers have been doing some form of Object Oriented Programming (OOP) for quite awhile now. Sometimes you might not even realize it. If you have been using VB 6 for example, then every control is a class, and you interact with it as an object by setting properties and calling methods on those objects. All you need to do now is to start creating your own classes, properties and methods.

Past Blog Content

Blog Archive