Mole v4 For Visual Studio – With Editing – Visualize All Project Types

December 31, 2007

Mole

Your wait is over!

Mole v4 for Visual Studio with Editing has been released.  This release allows developers to not only drill all around their Visual Studio applications to view their data, including private members, Mole now allows editing of these values.

You can read the article, download the code and a new .pdf manual at Code Project.

Mole’s Home Page:
http://karlshifflett.wordpress.com/mole-for-visual-studio/

Mole Project & Team Mole Page:
http://www.moleproject.com

Code Project Article:
http://www.codeproject.com/KB/macros/MoleForVisualStudioEdit.aspx

Enjoy Mole!

Just a grain of sand on the worlds beaches.


Updated WPF Sample Series – Custom Dialog (Vista and XP)

December 30, 2007

I have posted an update to the WPF Sample Series – Custom Dialog (Vista and XP).

I included an image of the dialog rendered in Windows XP and made a correction to the auto sizing of XP dialogs.

Please use the above link and download the updated code.

Just a grain of sand on the worlds beaches.


WPF Sample Series – Data Binding In ToolTip

December 29, 2007

This is the next sample in the WPF Sample Applications Series. The purpose of the Sample Series is to provide concise code solutions for specific programming tasks. This sample provides a brief description of the problem, the solution and full source code with a test bench program.

In a previous sample it was stated that, “One of the most powerful features in the WPF platform is its rich data binding.”  In this sample we will see how data binding can be used to display data bound values in a ToolTip.

ToolTips are really just little read-only popup windows that display some context sensitive information related to the object that the ToolTip belongs to.  With WPF’s very rich content rendering and control styling capabilities, ToolTips are not longer limited to a plain text.  Developers can very easily add images, 3D objects, paths, visuals, text, grids or just about anything to a ToolTip.  You can also make a ToolTip an irregular shaped window!  OK, just because we can, does not mean we should.  Oh well, I did this and have included a very cool Vista TaskBar application preview style ToolTip. 

ToolTips are controlled by a ToolTipService.  You can set ToolTipService attached properties on your ToolTips to control them.  The ToolTipService Class article covers the properties exposed by the ToolTipService Class and examples on using it to control the position of the ToolTip.  In fact, this sample uses the ToolTipService.SetHasDropShadow method to turn off the default drop shadow so that our cool Vista TaskBar ToolTip can use a different method for rendering its outer glow, just like Vista does.

 There are four methods that I know of that developers can use to code their ToolTips.  This sample demonstrates all four methods.

  • Entering a string in the ToolTip property of an object
  • Code the ToolTip in the controls XAML mark-up.
  • Code the ToolTip using XAML in a resource and apply that resource to one or more controls
  • Code the ToolTip in the code behind file, VB.NET or C#.

This sample will also cover using data binding in the 2nd and 3rd methods and how to use a VisualBrush in the 4th method.

 Sample Application

 application

When you first open the application, it looks like the above image.  I have entered my name and secret password.

Name ToolTip

textbox

At last, a ToolTip in a ToolTip article!  I have hovered my mouse over the Name TextBox and the cool data bound ToolTip pops up.  This is a very vanilla ToolTip from a design perspective but is rich in data binding.  Two properties Text and ActualWidth, from the TextBox control are data bound and rendered in the ToolTip.  You can data bind whatever properties your application needs to display.

Let’s have a look at the XAML mark-up resource that was used to render this ToolTip.  The blue lines of code will be commented on below.

<Window.Resources>
<ToolTip x:Key=”toolTipTextBoxBinding”
         DataContext=”{Binding Path=PlacementTarget,
           RelativeSource={x:Static RelativeSource.Self}}”>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=”Auto”/>
            <ColumnDefinition Width=”*”/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height=”Auto”/>
            <RowDefinition Height=”Auto”/>
            <RowDefinition Height=”Auto”/>
        </Grid.RowDefinitions>
        <Border HorizontalAlignment=”Stretch”
                BorderThickness=”0,0,0,1″ BorderBrush=”Black”
                Margin=”5″  Grid.Column=”0″
                Grid.Row=”0″ Grid.ColumnSpan=”2″>
            <TextBlock FontSize=”14″>
                 Two TextBox Data Bound Properties</TextBlock>
        </Border>
        <TextBlock Grid.Column=”0″ Grid.Row=”1″
                   Margin=”10,0,5,0″>TextBox.Text is:</TextBlock>
        <TextBlock Grid.Column=”1″ Grid.Row=”1″
                   FontWeight=”Bold” Text=”{Binding Path=Text}”/>
        <TextBlock Grid.Column=”0″ Grid.Row=”2″
                   Margin=”10,0,5,0″>TextBox.ActualWidth is:</TextBlock>
        <TextBlock Grid.Column=”1″ Grid.Row=”2″
                   FontWeight=”Bold” Text=”{Binding Path=ActualWidth}”/>
    </Grid>
</ToolTip>       
</Window.Resources>

The key piece of code is the DataContext property.  It’s here that the ToolTip’s DataContext is established, allowing child controls of the ToolTip to data bind to properties in the DataContext.  In the DataContext data binding code, PlacementTarget Property refers to the TextBox that opened the ToolTip.  The RelativeSource is set to the ToolTip.  So the ToolTip’s DataContext is itself, with the data binding Path being the TextBox that this ToolTip is associated with.  If you don’t have to reread this several times, you’re way smarter than me.

The second piece of code that makes this work, is located in the two TextBlocks with their Text properties data bound to properties on the TextBox control.  In this example we are data binding to the Text and ActualWidth properties.

So you can see how simple this really is.  The remained of the XAML mark-up is straightforward.  Check out how I used the border control to provide an underline that created a heading section for the ToolTip.  I could have used underlined text in the heading area, but this looks better.

Password ToolTip

password

This simple ToolTip using data binding to bind two properties Password and ActualWidth, from the PasswordBox control are data bound and rendered in the ToolTip.  Sometimes it is nice to be able to see what you are typing when the computer keeps telling you the passwords do not match.  Here the ToolTip exposes the value of the PasswordBox.Password property to the user.

Let’s have a look at the XAML mark-up that was used to render this ToolTip.  This XAML is not located in a resource, but is in-line with the PasswordBox control XAML.  The blue lines of code will be commented on below.

<PasswordBox x:Name=”txtPassword”
             VerticalAlignment=”Top”
             Grid.Column=”1″ Grid.Row=”2″
             d:LayoutOverrides=”VerticalAlignment”
             Margin=”0,0,0,10″ Grid.RowSpan=”1″>
   
  <PasswordBox.ToolTip>
    <ToolTip DataContext=”{Binding Path=PlacementTarget,
             RelativeSource={x:Static RelativeSource.Self}}”>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width=”Auto”/>
                <ColumnDefinition Width=”*”/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height=”Auto”/>
                <RowDefinition Height=”Auto”/>
                <RowDefinition Height=”Auto”/>
            </Grid.RowDefinitions>
           
            <Border HorizontalAlignment=”Stretch”
                    BorderThickness=”0,0,0,1″
                    BorderBrush=”Black” Margin=”5″ 
                    Grid.Column=”0″ Grid.Row=”0″
                    Grid.ColumnSpan=”2″>
                <TextBlock FontSize=”14″>
                    Two Password Data Bound Properties</TextBlock>
            </Border>
           
            <TextBlock Grid.Column=”0″
                       Grid.Row=”1″
                       Margin=”10,0,5,0″>
                PasswordBox.Password is:</TextBlock>
           
            <TextBlock Grid.Column=”1″
                       Grid.Row=”1″
                       FontWeight=”Bold”
                       Text=”{Binding Path=Password}”/>
           
            <TextBlock Grid.Column=”0″
                       Grid.Row=”2″
                       Margin=”10,0,5,0″>
                PasswordBox.ActualWidth is:</TextBlock>
           
            <TextBlock Grid.Column=”1″
                       Grid.Row=”2″
                       FontWeight=”Bold”
                       Text=”{Binding Path=ActualWidth}”/>
        </Grid>
    </ToolTip>
  </PasswordBox.ToolTip>
</PasswordBox>

This code is the same code used in the previous example with a few outer container changes.  You will typically see most fancy ToolTips coded as resources to promote code reuse.  However, you may find situations were placing the ToolTip XAML in with the control’s mark-up makes the most sense.

VisualBrush ToolTip Demo (The Big Kahuna)

open tooltip two

Now it’s time for some fun.  Use your mouse and resize the application and make it taller.  Notice how the image size grows and shrinks as you resize the form.  You have probably already seen it, but go ahead and place you mouse over the Window and you’ll see the cool ToolTip popup.  I wrote this ToolTip for my applications that use the TabControl that allows the user to have many screens open at once.  I wanted the ToolTip to resemble the ToolTip that the Vista TaskBar displays.  This is more for show, but it does look cool in a production business application.

What is going on here, is a rectangle is painted with a VisualBrush that is the visual from the Window Grid contents.  That rectangle is then surrounded with a border and a TextBox is placed above the border.  It took me about an hour of playing around with all the settings to get it to look like this.

This ToolTip is create in code.  The code is in the Window1.xaml.vb file.  You can simply copy the CreateToolTipForTabItem function right into your own code and use it.  I have marked several TODO’s in the code and provided suggestions for using this in your own code.  Here is how that function is called and the ToolTip created:

Me.ToolTip = CreateToolTipForTabItem(New VisualBrush(CType(Me.layoutRoot, UIElement)), “Vista Taskbar Style ToolTip For Window”)

This code snippet shows how to assign a ToolTip created in code to an objects ToolTip property.  In this case, it is the Window.  We create a New VisualBrush by down casting the Grid as a UIElement and pass this into the VisualBrush’s constructor.  The second parameter is the title string that appears above the ToolTip visual.

Now, go ahead and resize the Window again and cause the ToolTip to reopen again.  Um.  Make the Window small, open the ToolTip, now make is large and open the ToolTip.  What is going on here, is the visual from the Grid contents is painted on a 300 x 225 rectangle.  When the Window is small, the contents are scaled to fill the ToolTip.  For most business applications this scaling is not an issue since the forms are typically larger than 300 x 225.  You can also adjust anything about this solution to suit your needs.

This example ToolTip is a good example of what it takes to create a small form in code as opposed to XAML.  By the way, this ToolTip can also be created in XAML, I just wanted to demonstrate several methods for creating ToolTips.

Resources:  I am the original author for all this code.

Source Code: After downloading the source code you MUST change the file extension from .zip.DOC to .zip. This is a requirment of WordPress.com.

Download Source Code 32KB

Hope you can learn just a little bit more about WPF from this article and the Sample Series.

Just a grain of sand on the worlds beaches.


WPF Common TaskDialog for Vista and XP

December 28, 2007

I had recently posted the WPF Sample Series – Custom Dialog (Vista and XP) here on my blog.  I got way more hits and downloads for this post than I expected so I’ve expanded the sample to a longer article on Code Project.  I added more details and diagrams in the article.  Thank you for your interest in the Custom Dialog!

The Code project article can be view here:

http://www.codeproject.com/KB/WPF/WPFTaskDialogVistaAndXP.aspx

Hope you can learn just a little bit more about WPF from this article and the Sample Series.

Just a grain of sand on the worlds beaches.


Applications = Code + Markup (Charles Petzold) Visual Basic Code Sample

December 27, 2007

Great news for all the VB.NET WPF developers out there who have purchased (hopefully read) Mr. Petzold’s book, “Applications = Code + Markup”

The Visual Basic Team announced the source code in the book is being translated from C# to VB.NET.

At the present, about 1/2 the book has been translated.

You can download the code and follow the progress here:

http://blogs.msdn.com/vbteam/pages/Petzold-WPF-VB-Samples.aspx

Just a grain of sand on the worlds beaches.


WPF Sample Series – Custom Dialog (Vista and XP)

December 27, 2007

Sample Updated on 12/30/2007 

This article was awarded the Code Project VB.NET Article Award Dec 2007.

This is the next sample in the WPF Sample Applications Series.  The purpose of the Sample Series is to provide concise code solutions for specific programming tasks.  This sample provides a brief description of the problem, the solution and full source code with a test bench program.

This sample covers a very important topic, dialog boxes.  With the release of Vista also came the cool TaskDialog that all Vista users are getting used to.  It provides a richer dialog than the standard message box dialog available on Windows XP. 

However, this great new feature causes a minor bump in the carpet for WPF developers.   Which dialog should WPF developers use?  Should WPF developers customize their dialogs for each operating system?  What about application documentation?  (Can you image having to document two flavors of every dialog box.)  One solution clearly provides for much more information than the other, so what to do?

This is the very question that tapped on my shoulder about three months ago.  So I developed a custom dialog class that works on both Vista and XP.  It looks just like Vista’s TaskDialog, yet without all the messy API business.  (Micrsoft, please add TaskDialog to the 3.6 Framework so we WPF developers do not need to fool with the Windows API.)  If you’re interested, you can have a look at the VistaBridge application in the Windows SDK.

Note
After I initially posted this Sample, I had way more views and downloads of this post than I expected. So I have expanded this post to an article on Code Project. The article there are class diagrams and a few areas have been expanded.

The Code project article can be view here:
http://www.codeproject.com/KB/WPF/WPFTaskDialogVistaAndXP.aspx

Sample Program

Sample Program

Sample One (Vista)

Sample One

Sample One (Windows XP)

 xpsample

Sample Two

Sample Two

Sample Three

Sample Three

Features

  • Same simple interface for WPF developers.  Works the same on Vista and XP – simplifies programming
  • Common icons and text for standard documentation for Vista and XP
  • Rich dialog box capabilities
  • Detailed user dialog usage logging capabilities built in. (see TODO in project)
  • Optional delay feature
  • Dialog box must be closed by clicking on a button.  (ALT-F4 is also disabled)

Usage Example

  Dim obj As New CommonDialog.CustomDialog
  obj.AdditionalDetailsText = “These are additional details”
  obj.Buttons = CommonDialog.CustomDialogButtons.OKCancel
  obj.ButtonsDisabledDelay = 5
  obj.Caption = “The buttons are disabled for 5 seconds”
  obj.DefaultButton = CommonDialog.CustomDialogResults.OK
  obj.FooterIcon = CommonDialog.CustomDialogIcons.Information
  obj.FooterText = “This is the footer text”
  obj.InstructionHeading = “This is the instruction heading”
  obj.InstructionIcon = CommonDialog.CustomDialogIcons.Question
  obj.InstructionText = “Do you want to proceed with the next task?”
  Dim objResults As CommonDialog.CustomDialogResults = obj.ShowMe.tbResults.Text = String.Format(“Last dialog result was {0}”, objResults.ToString)

Let me touch on the optional delay feature.  Many times our beloved users are quick on the keyboard or mouse and just click through important dialog boxes.  They tend to tell customer support, “I never saw that box!”  For those users ( I can smell a new user profile setting ), or for very important dialog boxes, I added the delay property. 

Just assign how many seconds you want the buttons to be disabled before the user can respond to the dialog box.  Since this dialog can’t be closed with the little red X or by pressing ALT-F4, your user will have plenty of time to actually read and digest the information you are trying to communicate.  Just don’t get to crazy with the delay feature.

The logging feature is also very cool.  Get into the source code and check the project TODO comment.  This custom dialog has many properties that can be logged including stack trace information so that developers and support staff can easily determine who, what, where, when and why dialog information. 

Resources: I am the original author for all this code.

Source Code: After downloading the source code you MUST change the file extension from .zip.DOC to .zip. This is a requirment of WordPress.com.

Download Source Code 104KB (Updated 12/30/2007)

Hope you can learn just a little bit more about WPF from the Sample Series.

Just a grain of sand on the worlds beaches.


WPF Sample Series – Formatting Data Binding Dates and Numbers

December 27, 2007

This will be the first in a series of WPF Sample Applications I’ll be posting over the next year or so.

The purpose of the Sample Series is to provide concise code solutions for specific programming tasks.  This sample provides a brief description of the problem, the solution and full source code that includes a working program with many examples of actually using the sample code. 

Let’s hit the ground running with a very common WPF programming task, formatting data binding expressions.

One of the most powerful features in the WPF platform is it’s rich data binding.  WPF makes it very easy to perform declarative data binding in the XAML mark-up along with data binding in code.   When we hear or read, “data binding” we normally associate this with data binding to a database, web service or business layer than exposes the database.  In WPF, data binding goes much further.  For example a developer can data bind a property in an object to another dependency property in the same object or to a dependency property in another object.  WPF data binding allows the developer to determine when in the application life cycle the value will be updated and the direction of the binding operation.

That being said, this short sample covers a small but important piece of the data binding feature that allows for the formatting of data by using a converter.  WPF data binding coverters allow developers to do exactly that.  Take a value from a source and convert it to something else during the data binding process so that the target receives the converted value.

In this sample, we will take advantage of the data binding converter plumbing to format numeric and date data.  Our converter uses the standard .NET Framework numeric and date formatting expressions that all .NET developers are familar with and converts the numeric and date data from the source to a formatted string value that gets displayed in the TextBlocks in the application.

Converters give the developer full control on how data is displayed to the user.  In the sample program below, you’ll see many examples of displaying three properties, a date, a double and an integer.

Sample Program 

Formatting Sample

Description:  From the above image, it is easy to see where this sample is taking us.  The column on the left identifies the property and the formatting string used to display the value in the right colomn.  I don’t know about you, but for me, a picture is worth a thousand words.

The data source is a single instance of the SampleDataClass that has four properties, Name, Birthday, Salary and Age.  These values are set in the constructor of the Window.  The data class has been assigned as the DataContext for the Grid that contains the child TextBlock controls.  The alternating grid row background color was implemented by placing a Rectangle in every other row with the Fill set to AntiqueWhite.

The various properties are bound to the TextBlock Text property using binding expressions that include a converter.  The converter handles the formatting duties. 

Here are three sample binding expressions from the program:

Text=”{Binding Path=BirthDay, Converter={StaticResource FormattingConverter},
  ConverterParameter=\{0:M\}, Mode=Default}”
Text=”{Binding Path=BirthDay, Converter={StaticResource FormattingConverter},
  ConverterParameter=\{0:R\}, Mode=Default}”
Text=”{Binding Path=BirthDay, Converter={StaticResource FormattingConverter},
  ConverterParameter=’{}{0:ddd, d MMM yyyy} is my day!’, Mode=Default}”

Notice that in the first two examples the ConverterParameter has been escaped \{ and \}. 

The third example uses an alternate syntax that does not require escaping.  By proceeding the parameter with {}, escaping is not required.  Also note, the third example demonstrates how to include a string along with the binding expression, “is my day!” has been added to my birthday.

The comments have been removed from the below code example but are in the source code download.   (I’m also trying out using an image for the displayed code.)

New Sample Code
This converter provides the ability to convert back by using a type specific TypeConverter. 

Resources:  I used the MSDN documentation and several examples from the Internet along with code I use in my production applications to make this sample.

Source Code: After downloading the source code you MUST change the file extension from .zip.DOC to .zip.  This is a requirment of WordPress.com

Source Code 57KB

Hope you can learn just a little bit more about WPF from the Sample Series.

Just a grain of sand on the worlds beaches.