Featured Posts

WPF and the Event Routing Paradigm, with Bacon Since the first event-driven language came down from the mountain and became part of the software development vernacular, there has been a certain notion of how an event works. Subscribe...

Read more

How to stop Expression Blend 4 RC from crashing on... So you’ve downloaded Expression Blend 4 RC but when you start it up, Blend crashes hard.  It’s never done that before, right? What do you do?  Well, you can...

Read more

The WPF Must-Grok List Robert A. Heinlein defined the word grok as: Grok means to understand so thoroughly that the observer becomes a part of the observed—to merge, blend, intermarry, lose identity...

Read more

Yes, Virginia, TextBlock.Text Will Support MultiBinding One of the features introduced to WPF in version 3.5 SP1 is the StringFormat property. If you’re unaware, StringFormat allows you to bring the String.Format capability...

Read more

Why Are WPF Developers Trying to Kill Menus? Somewhere along the line, and I’m not motivated enough to research it, menus apparently became bad.  I don’t mean they ever stopped working or became unsuitable for the...

Read more

using System.Windows; Rss

WPF On Your Shelf: Programming WPF, revisited 3 years later

Posted on : 05-25-2010 | By : Christopher Estep | In : Books

Tags: ,

0

I have a lot of technical books, particularly on WPF. I have most of the books released by the major publishers on WPF, unless it’s about VB. There are 2 books that are my absolute favorites and the first one is Programming WPF by Chris Sells and Ian Griffiths, published by O’Reilly in 2007.

 

Programming WPF

Why am I reviewing a 3 year old book? It is precisely because this book is 3 years old that I’ve chosen to review it now simply because the book is that good. Of all of the books that I use, this one has helped me more than any, without question. And it still helps me. 

Yesterday, when I was writing my post WPF and the Event Routing Paradigm, with Bacon, I went through my books to make sure that I was not only being accurate, but thorough. I wanted not only the conceptual overview and walkthrough, but plenty of technical meat so I could be certain that what I was writing would be truly useful for the people for whom I’ve written it. More importantly, I didn’t want any gaps or omissions.

Many of the books gave only a cursory explanation of Routed Events, something I find to be mindboggling. WPF isn’t easy and Routed Events aren’t intuitive. That’s why I chose the “racing pigs” metaphor. Makes you want to go look, doesn’t it? :) Of all of the book, only WPF had the technical depth I was looking for.

But it’s 3 years old!

So? Yes, we’re at WPF 4 and 3 versions of .NET later (3.5, 3.5 SP1, and 4.0), but the information in Programming WPF is simply that good that it doesn’t really matter. Most of what’s changed in WPF recently have been additions and not breaking changes. The books strength isn’t that it’s on the bleeding edge of technology, it’s that it leaves no stone unturned in what it does cover.

Positives

  • In depth – It covers its topics with such depth that it is very nearly a reference book on par with MSDN articles on the subject.
  • Broad – There are very few areas of WPF that are left untouched, including 3-D, printing, and text processing.
  • Accessible – The writing is very clear and understandable. Many writers may know their subject but no matter how many books they write, they still can’t quite explain their subject clearly. Griffiths and Sells don’t suffer with that problem.
  • Indispensable – This book is truly useful. It’s one you will pick up again and again until its pages are worn.
  • It’s heavy – At 835 pages through the index, think of the exercise you’ll get! It is exercise and it counts. That’s my story and I’m sticking to it.

Negatives

  • It’s dated – I have to say it. Some subjects just aren’t covered because they didn’t exist when the book is written. I can’t find that there’s going to be a 3rd edition, which would be a shame.
  • Silverlight chapter is useless – Sorry, Shawn. It’s just because it’s dated (see above).

I honestly can’t say enough good things about this book. It has helped me immensely over the past few years and it still is. If you don’t have Programming WPF, you seriously should consider getting it.

Popularity: 10%

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DotNetKicks
  • StumbleUpon
  • LinkedIn
  • Facebook
  • TwitThis
  • Digg
  • DZone
  • Reddit
  • Technorati
  • email
  • Print

WPF and the Event Routing Paradigm, with Bacon

Posted on : 05-24-2010 | By : Christopher Estep | In : Basics

Tags:

1

Since the first event-driven language came down from the mountain and became part of the software development vernacular, there has been a certain notion of how an event works.

Subscribe to Event Subscribe to event
Fire that event! Event fires
Event Handling in Forms is easy, but messy Deal with event
Direct Event Goodness! Breakfast!

All in all, it’s pretty simple.  And life was good.

So along comes WPF and things start to get a little more complicated. Events no longer have a simple one-to-one relationship. The event cycle may not be what you expect.

Event Subscription Subscribe to event
Fire that event! Event fires
Events bubble and multiply Events everywhere!
Event Handling doesn't have to be messy Deal with event
Runaway Events Events still running away!

As I said, it’s not quite what you expect. Previous to WPF, event handling was very direct and closed. An event went where it was told and that was that. With WPF, events have a mind of their own, or so it would seem.  But once you begin to understand what happens and why, WPF’s event-handling paradigm begins to make a lot more sense. And this paradigm is called Routed Events.

Manual versus Automatic

Windows Forms style event-handling is very manual. You subscribe to an event, deal with the event and it’s over, whether you like it or not. On one level, it means there are less pieces to worry about, less things that could go wrong. But the more you work with that style of event-handling, you realize how much your handcuffed by it.

To demonstrate the Windows Forms or CLR approach, I’ve made a Forms application to demonstrate how event processing occurred previously (and still does in non-WPF & non-Silverlight applications)

RoutedEventWinForm-1

And the following code-behind:

public partial class Form1 : Form

{

    public Form1()

    {

        InitializeComponent();

        button1.GotFocus += new EventHandler(button1_GotFocus);

        button2.GotFocus += new EventHandler(button2_GotFocus);

        tabControl1.GotFocus += new EventHandler(tabControl1_GotFocus);

    }

    void tabControl1_GotFocus(object sender, EventArgs e)

    {

        textBox1.Text += String.Format("tabControl1 GotFocus{0}", Environment.NewLine);

    }

    void button2_GotFocus(object sender, EventArgs e)

    {

        textBox1.Text += String.Format("Button2 GotFocus{0}", Environment.NewLine);

    }

    void button1_GotFocus(object sender, EventArgs e)

    {

        textBox1.Text += String.Format("Button1 GotFocus{0}",Environment.NewLine);

    }

}

Pretty simple.  When you it, you get:

RoutedEventWinForm-2

All I did was tab through the controls and it printed exactly one line each time.

This is because normal events are “one and done” in the sense that once they are dealt with, they are considered to be handled and it’s all over.

Now let’s take a look what happens in WPF.  I made a simple form in WPF (if anything in WPF can be considered simple, but this one actually is) and wired the GotFocus event for each of the controls, but I also wired it up for the form itself.

RoutedEventWPF-1

The XAML:

<Window x:Class="WPFEventDemo.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525" GotFocus="Window_GotFocus">

    <Canvas>

        <TabControl Canvas.Left="80" Canvas.Top="46" Height="100" Name="tabControl1" Width="200" GotFocus="tabControl1_GotFocus">

            <TabItem Header="tabItem1" Name="tabItem1" GotFocus="tabItem1_GotFocus">

                <Grid GotFocus="Grid_GotFocus">

                    <Button Content="Button1" Height="23" HorizontalAlignment="Left" Margin="20,11,0,0" Name="button1" VerticalAlignment="Top" Width="75" GotFocus="button1_GotFocus" />

                    <Button Content="Button2" Height="23" HorizontalAlignment="Left" Margin="20,39,0,0" Name="button2" VerticalAlignment="Top" Width="75" GotFocus="button2_GotFocus" />

                </Grid>

            </TabItem>

            <TabItem Header="tabItem2" Name="tabItem2">

                <Grid />

            </TabItem>

        </TabControl>

        <ScrollViewer Height="116" Canvas.Left="28" Canvas.Top="172">

            <TextBlock  Name="tb" Text="" Width="446" />

        </ScrollViewer>

    </Canvas>

</Window>

And the code-behind:

public partial class MainWindow : Window

{

    public MainWindow()

    {

        InitializeComponent();

    }

    private void button1_GotFocus(object sender, RoutedEventArgs e)

    {

        tb.Text += String.Format("Button1 GotFocus \n");

    }

    private void button2_GotFocus(object sender, RoutedEventArgs e)

    {

        tb.Text += String.Format("Button2 GotFocus \n");

    }

    private void Grid_GotFocus(object sender, RoutedEventArgs e)

    {

        tb.Text += String.Format("Tab 1 Grid GotFocus \n");

    }

    private void tabItem1_GotFocus(object sender, RoutedEventArgs e)

    {

        tb.Text += String.Format("tabItem1 GotFocus \n");

    }

    private void tabControl1_GotFocus(object sender, RoutedEventArgs e)

    {

        tb.Text += String.Format("tabControl1 GotFocus \n");

    }

    private void Window_GotFocus(object sender, RoutedEventArgs e)

    {

        tb.Text += String.Format("----------------------------\n");

    }

}

And when you run it you get:

RoutedEventWPF-2

with the following in the TextBlock:

tabItem1 GotFocus

tabControl1 GotFocus

—————————-

Button1 GotFocus

Tab 1 Grid GotFocus

tabItem1 GotFocus

tabControl1 GotFocus

—————————-

Button2 GotFocus

Tab 1 Grid GotFocus

tabItem1 GotFocus

tabControl1 GotFocus

—————————-

Each section is the result of a single press of the tab key, including the dashed line. What you’re seeing is the result of event bubbling and this happens every time an event is fired in WPF and Silverlight.  Look at the second group. When I tabbed to the button, it fired GotFocus for the deepest, nested object and then bubbled up doing likewise to each object in the tree until there were no more left. In fact, the GotFocus event for MainWindow is what added the dashed line at the end.

But what if you don’t want that behavior?  That’s simple enough. You may have noticed that I haven’t been using the term “handle” for dealing with events. This was intentional, because “handled” has a specific meaning in RoutedEvent processing and now you’re going to learn what.

If you don’t want events to bubble up past a certain point, all you need to do is set Handled=true for RoutedEventArgs in the handler. At that point the bubbling stops and WPF truly considers the event completely handled. What is key is that I said “past a certain point”. You can stop the bubbling anywhere in the hierarchy, not just at the control that caused the event.

In the following code, I’ve set Handled to true at Button1 and tabItem1.  Watch what happens.

RoutedEventWPF-3

Again, I tabbed 3 times. The first tab gave focus to tabItem1 and since I set Handled to true in tabItem1, that’s all it printed. The second tab went to Button1 and since Handled is true in that method, it also just prints one line. But I didn’t set anything in Button2 so when I tabbed to that control, it ran the handlers for Button1, the unnamed grid that is it’s parent and tabItem1, again stopping.

It’s important to note that if I had clicked tabItem2, it would have printed “tabControl1 GotFocus” and a dashed line because I never removed those handlers.

Conditional handling

I put the following code in the the tabControl1 GotFocus handler method:

private void tabControl1_GotFocus(object sender, RoutedEventArgs e)

{

    Control osControl = e.Source as Control;

    if (osControl.Name == "button1" || osControl.Name == "tabItem2")

    {

        e.Handled = true;

    }

}

And what you’ve got is a very simple way of conditionally deciding if you want the event to keep bubbling all the way up the tree.

I hope I’ve given you enough information so that in your own events processing you can turn this…

Runaway Events!

into this…

Event Handling Tastes Like Bacon!

(ALTERNATE VERSION for vegetarians)

I hope I’ve given you enough information so that in your own events processing you can turn this…

Evil Carrot

into this…

Event Salad!

[Side note: Yes, I know I only talked about Bubbling and WPF also supports Tunneling.  I’ll cover Tunneling in a future post!]

[Side note 2: Yes, I also know that Routed Events are not a new subject in the WPF and Silverlight world, but I was asked about them and it's never a bad time to go over the fundamentals.]

kick it on DotNetKicks.com

Popularity: 30%

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DotNetKicks
  • StumbleUpon
  • LinkedIn
  • Facebook
  • TwitThis
  • Digg
  • DZone
  • Reddit
  • Technorati
  • email
  • Print

Are 2 syllables slowing you down?

Posted on : 05-15-2010 | By : Christopher Estep | In : Opinion

Tags:

0

Rant alert.

In the English language, all of the letters of the alphabet have one syllable. Well, except for one. And that letter is the dreaded W.

In the world of computers, we have acronyms for almost everything, and what we doesn’t have a TLA has an abbreviation instead. Unfortunately, since Microsoft chose to name their flagship product Windows, much of those TLA’s start with the dreaded W.

Whether software developers are efficient or just lazy is a topic for debate. As I will note, I tend toward believing in the “lazy” angle.

What is this letter?

W

Say it out loud.

Do you say “double – you”?  Of course you do. You don’t say “dub”. Nobody says “dub”.

So tell me, why in the world would you ever pronounce “WCF” as “dub see eff”?!

Are you that lazy?  Really?  Are the syllables “bull you” really slowing you down so much that you have to just drop them? Is your tongue so tied and twisted that you can’t say them fast enough?  I picture this exchange.

Bob: I learned a tongue twister today.Seven silver swans swam silently seaward.

Jane: That’s nothing. Try “WCF and WF”

Bob: Stop!!! It’s too hard!!!

Ok, I’m done with my rant.  But let’s be real. saying “dub-eff” or “dub see eff” just makes you look stupid.

Friends don’t let friends butcher the language.

Popularity: 8%

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DotNetKicks
  • StumbleUpon
  • LinkedIn
  • Facebook
  • TwitThis
  • Digg
  • DZone
  • Reddit
  • Technorati
  • email
  • Print

How to stop Expression Blend 4 RC from crashing on startup

Posted on : 04-25-2010 | By : Christopher Estep | In : Silverlight

Tags: , , , ,

1

So you’ve downloaded Expression Blend 4 RC but when you start it up, Blend crashes hard.  It’s never done that before, right?

What do you do?  Well, you can do what I and a few others did and spend hours uninstalling and reinstalling various software in the (vain) hope that it will solve your problem.  It won’t.

Or you can learn from my experience and solve the problem in a matter of minutes.

Let me ask you first, do you have OpenOffice on your computer or have you ever had it? I can say with a degree of certainty that therein lies your problem.

Apparently, there’s a typeface called “Deja Vu” from Bitstream that Blend 4 RC doesn’t like. And by “doesn’t like” I mean, “makes your program go boom”.

There’s an easy fix.  Delete the font files. That’s all there is to it.  Since Blend is a technical tool, I’m not going to tell you how to delete the files.  If you don’t know how, it’s worth your time to figure it out.

If you want to know how I figured this out and get a little more detail on the error messages, go over to this thread where my answer was originally posted.

And remember, friends don’t let friends use OpenOffice. :)

Update

Microsoft has fixed the problem and released an update to Expression Blend 4. I haven’t tested it myself (since I removed the offending font) but hopefully this does the trick!

Popularity: 24%

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DotNetKicks
  • StumbleUpon
  • LinkedIn
  • Facebook
  • TwitThis
  • Digg
  • DZone
  • Reddit
  • Technorati
  • email
  • Print

Absent from my Grok List

Posted on : 02-11-2010 | By : Christopher Estep | In : Basics

2

It’s been suggested that my WPF Must-Grok List is “basically the whole WPF” so in my own defense, I thought I’d list a few items to illustrate how WPF is far deeper than my list:

  1. Attached Properties
  2. Visual & Logical Trees
  3. MVVM
  4. Prism
  5. Click-once deployment
  6. Browser apps
  7. Animation
  8. Graphics (2D)
  9. 3D Graphics
  10. Flow Documents & Text
  11. Ink
  12. Printing & XPS
  13. Media
  14. Adorners
  15. Custom controls
  16. Interop

And even this is not a complete list.

Remember, WPF is a new paradigm in software UI development. It does have a significant learning curve and it is a huge mistake to oversimplify it.

Popularity: 26%

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DotNetKicks
  • StumbleUpon
  • LinkedIn
  • Facebook
  • TwitThis
  • Digg
  • DZone
  • Reddit
  • Technorati
  • email
  • Print