October 6, 2006 .net

WPF: Enabling Crimes Against Nature in a Good Way

WPF: Enabling Crimes Against Nature in a Good Way

My friend Jeff asked me to do a terrible thing yesterday: How do I show a form in the popup window that a menu shows?” I said, Dude! Don’t do that — write a dialog!” And that’s what he did, but then, like a train wreck, I couldn’t look away. It took me all of 10 minutes of very straight forward coding and here we are:

If you’re familiar with WPF, data binding and data templates, this is all standard stuff. Here’s the data structure:

class Person : INotifyPropertyChanged
{
    string name;
    public string Name
    {
        get { return name; }
        set { name = value; Notify("Name"); }
    }
    int age;
    public int Age
    {
        get { return age; }
        set { age = value; Notify("Age"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    void Notify(string prop) { if( PropertyChanged != null ) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } }
}

The data template is also bog standard:

<DataTemplate DataType="{x:Type local:Person}">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="auto" />
      <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="auto" />
      <ColumnDefinition Width="auto" />
    </Grid.ColumnDefinitions>
    <Label Grid.Row="0" Grid.Column="0">_Name</Label>
    <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Name}" />
    <Label Grid.Row="1" Grid.Column="0">_Age</Label>
    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Age}" />
  </Grid>
</DataTemplate>

Setting up the app itself is nothing special:

<Window ...>
  <Window.Resources>
    <DataTemplate DataType="{x:Type local:Person}">
      ...
    </DataTemplate>
  </Window.Resources>
  <Grid>
    <Menu>
      <MenuItem Header="File">
        <MenuItem Header="Exit" x:Name="fileExitMenuItem" />
      </MenuItem>
      <MenuItem Header="Form" ItemsSource="{Binding}" />
    </Menu>
  </Grid>
</Window>

Notice that the items that make up the menu come from a binding to the ambient data source, which we set in the code-behind:

protected override void OnInitialized(EventArgs e)
{
    ...
    Person tom = new Person();
    tom.Name = "Tom";
    tom.Age = 11;
    DataContext = new Person[] { tom };
}

The only thing at all tricky is that, even though we’re only editing a single object in the popdown window,” it has to be in an implementation of IEnumerable so that the menu can display it (it expects items to come in a list).

That’s it. You can download the RC1 WPF project here.

You might wonder why I would post such a sample, when clearly that’s not a UI we want to foster on folks. The answer is simple: because WPF allows it.

The power of any platform should be judged by how well it lets you write bad code. Early versions of Visual Basic let you write really bad code — but it worked. Likewise, WPF lets me build the worst UIs ever — but they work. This is necessary so that UI visionaries can experiment and get to a place where they’re doing something completely different from UIs of today and we like it.

Do you think the Office folks, who, for all practical purposes, have been setting UI guidelines on Windows for a decade, could use MFC or Windows Forms? No, because they’re not flexible enough. Could they use WPF? Absolutely they could. And so can you.

The crayon box has lots of colors that I don’t like, but in the hands of an artist, they can create beauty.

October 2, 2006 fun

Teaching Kids To Do Cool Computer Stuff?

My youngest son’s teacher found out I work at MS, so now he expects me to come into his class to teach them cool stuff. I’m happy to do that, but now I’m worried about putting together a cool 1-4 hours of stuff to do, e.g.

  • Cool PowerPoint stuff (wowing their classmates)
  • Cool Excel stuff (doing their math homework)
  • Cool Web stuff (building their own web site)
  • Cool Programming stuff (writing their own PC/XBox 360 games)
  • Cool Hardware stuff (programming a robot)

Obviously, I’m not the first person to want to teach kids how to do cool stuff w/ their computers. Can someone point me at some activities for a classroom for of kids to do in these (or other) categories? Thanks!

P.S. My son is a 6th grader, making his class 11 and 12 year olds.

October 1, 2006 .net

Sept CTP of .NET 3.0

The CTPs are coming so fast and so furious now that I nearly missed the September 2006 .NET Framework 3.0 CTP:

Enjoy!

October 1, 2006 fun

Null Pointers Don’t Have To Be Dull Pointers

Null Pointers Don’t Have To Be Dull Pointers

Posted without permission after reading it in today’s paper
Sun 10/01/2006 3:01pm

September 27, 2006 spout

Horning in on Hanselminutes

It took a while, but I was finally able to horn in on an episode of Hanselminutes. Thanks for having me, Scott!
September 8, 2006 spout

Amazon Unbox — what about my TV?

I don’t want to download movies via Vongo or Amazon Unbox to my PC; I want to download them to my TV and smart phone! YouTube works because the videos are short, so watching them on my PV happens right now or not at all. Any movie or TV show service that doesn’t let me play the result on my media center or take it with me on my phone, I just don’t care. Does anyone want to watch 30+ minutes of video sitting at their desk or staring at their lap? I don’t get it…

September 7, 2006 .net

.NET 3.0 RC1 Download

September 5, 2006 spout writing

Congrats To Mr. Petzold on this WPF Review

In the world of Windows technical writing which has so much competition, there’s rarely any money involved, one dreams for reviews like these from KarstenJ:

Tim Sneath walked into my office the other day and laid Charles Petzold’s Applications = Code + Markup on my desk.  I’m only to Chapter 7 of 31 chapters and I am riveted.  I already have that feeling when reading a great novel when you don’t want it to end.  It actually does read like a novel to me, with a narrative arch as it negotiates its methodical way through the WPF jungle of APIs.”

Congrats, Charles.


← Newer Entries Older Entries →