Windows Presentation Foundation SDK : ItemControls don’t properly select items or raise events when bound to a data source

|

When doing my daily RSS catch-up (I'm loving at the moment), I came across this:

Windows Presentation Foundation SDK : ItemControls don’t properly select items or raise events when bound to a data source

It's only a short article but it points out something very important when working with data templates in WPF: it's a template. I know that sounds obvious, but it's easy to forget.

Setting events, by using EventSetter, is not as natural as setting them directly in the template XAML with Click="blah" but attaching events to the cookie cutter and not the cookie will give unexpected results, so don't get burned (I know my analogy sucks, feel free to comment something better).

Tufte has arrived...

|
TufteBook

This exceptional book written by Edward R. Tufte, recommended to me by Mr Mark Miller (also here), of DevExpress / CodeRush / Refactor! fame, has arrived today.

It looks lovely, even the packaging looks great, which is promising - I'm really looking forward to getting my head into it and improving my design skills.

Here is the description of the book on the Tufte Web site:

Visual Explanations: Images and Quantities, Evidence and Narrative is about pictures of verbs, the representation of mechanism and motion, process and dynamics, causes and effects, explanation and narrative. Practical applications and examples include statistical graphics, charts for making important decisions in engineering and medicine, technical manuals, diagrams, design of computer interfaces and websites and on-line manuals, animations and scientific visualizations, techniques for talks, and design strategies for enhancing the rate of information transfer in print, presentations, and computer screens.

Yeah, I didn't really understand it either.

As I read through it though, I'm sure I'll be blogging my thoughts. However, if anyone has already read it I would love to hear your thoughts and please feel free to offer other suggested reading.

Now I just need a quiet few hours to myself.....

Change notification for a DependencyProperty you don't own

|

We all know that when you create a DependencyProperty you get change notification for free (amongst other things), which is great when you are the owner of the property because you can setup change event handlers (which I have posted about in the past). You can even override the metadata for a DependencyProperty when you inherit from a class that owns a property you want notification for; injecting your change event handler. But, what about the time when you do own the property directly or via inheritance, how do you get change notification then?

The answer is to use a PropertyDescriptor - this is the class that Visual Studio and similar tooling would use to keep track of property values in the designer etc. - this is nice feature that you can exploit in your code. Given the property descriptors primary purpose it's not that surprising to find it in the System.ComponentModel namespace. Note that a property descriptor has been written specifically for dependency properties, to give additional information about the property, therefore to get a reference to one is as simple as:

DependencyPropertyDescriptor descriptor =
  DependencyPropertyDescriptor.FromProperty
    (UIElement.VisibilityProperty, typeof(UIElement));

descriptor.AddValueChanged
  (this.labelShowHide, new EventHandler(VisibilityChanged));

This is actually only two lines of code, though it may look more. First we get a reference to the dependency property that we care about (VisibilityProperty in this instance) and then add an event handler to the ValueChanged event for a DependencyObject instance (a TextBlock called labelShowHide in this example) as well as supplying the event handler we would like to be invoked when the value has changed.

Lovely. However, now for the bad news, there are two issues with this implementation that may not be obvious right away. Firstly, notice that the event handler is only a simple EventHandler, therefore the event arguments will only be an EventArgs instance; therefore the rich change notification supplied by the "owner" scenarios, with old and new values being supplied with the event, are not available to you with this approach. Secondly, this implementation has a leak, a memory leak. The reference to your callback forever roots your class, as the reference is stored in a static Hashtable (# dun, dun, daahhhh! #)

There are a number of ways to deal with these issues depending on your scenario. In most cases I would suggest that you can safely ignore both.

However, if you *really* want to plug the leak then take a look at this great article by Andrew Smith; however, in all but the most complex cases I would suggest that when you no longer care about the notification for the target property it will also be right about the time your application is about to close; at which point everything will be reclaimed anyway.

To solve the change notification you could simply have a dictionary, keyed on the target object instance, storing the last known value for the target property. In all but a few edge cases you'll know the controls that you're watching, so this solution is simple and easy to implement:

// Define an internal dictionary to store the values
private Dictionary _currentValues =
   new Dictionary();

public MainWindow()
{
    this.InitializeComponent();

    // Descriptor code goes here...

    // Set the initial values in the constructor.
    this._currentValues.Add
        (this.labelShowHide, this.labelShowHide.Visibility);

}

// Change Event Handler
private void VisibilityChanged(object sender, EventArgs e)
{
    // Extract the values.
    object oldValue = this._currentValues[sender];
    object newValue = this.labelShowHide.Visibility;

   

    // Do something with the data.
    MessageBox.Show("Value changed from \"" +
        oldValue.ToString() + "\" to \"" +
        newValue.ToString() + "\"");
   

    // Update the values list with the new value.
    this._currentValues[sender] = newValue;
}

All we have here is a simple dictionary to store the current value for control, so we have it cached for when it changes, and a simple little piece of boiler plate  in the event handler to extract the old value and update the dictionary with the new value. Obviously if you're monitoring many controls in this way you could easily factor out the boiler plate to a generic method or add another level of indirection by raising your own property changed event using the DependencyPropertyChangedEventArgs. Also, if you want to monitor many properties on the same control you'll have to invent another key.

So in summary, the simplest way to hook into the change notification for a DependencyProperty you do not or cannot own in some way is to use the PropertyDescriptor mechanism. In most cases the amount of code required to deal with the memory leak is not worth the effort (but be aware of it) and if you really need the old/new value functionality simply create a dictionary to store the old values for you.

I hope this helps.

Holiday

|
I have been away on holiday for the last week or so, in Keswick, Cumbria, England - a truly wonderful place.

The picture is a "live" picture of Keswick from the top of the building looking at Skiddaw.

Unified Communications - Conclusion

|

As I said in my previous post, this post will cover the details of what actually happened on day 4, over what I predicted; along with my conclusion for the conference.

Day 4, the reality

Day 4 started with breakfast, a trip to the Birds of a Feather sessions and a quick walk around the impromptu sessions in a place called "Pub World". This was very interesting, to hear all the IT pros in their discussion, but there was very little for the developer, that I could find anyway.

With that I decided there was little more left for me to eek out of the conference and decided to head off home. This decision came a fair number of hours before my flight was due to leave from San Diego, so the adventure of the journey home began early. Also, over the last few days I had been reading in papers about the issues that American Airlines had been having with their jets and the FAA, grounding many, many flights - this obviously played on my mind, so I thought leaving early might help. It did. I ended up getting a shuttle bus to LAX and caught a slightly earlier flight in to London Heathrow, which was great, it gave me some time to get ready for my holiday the next morning.

Conference Conclusion
Overall I really enjoyed the conference, it was well organised and well done. I think there was only really one day for developers (which was not that clear to me before I left); but the other days were days well spent getting an understanding, or Zen, of unified communications and all that it entails, including both software and hardware.

Unified Communications - Day 4

|
 INTERACT2008BlogArt2

This post is a little different to others, as I'm spending so much time travelling over the next couple of days; at the end of which I'll be miles away from a computer, for over a week, I cannot wait!

Anyway, so I thought I'd post what was supposed to happen today, and then, when I get back to my desk in a week or so I'd do one last post about the UC conference summarizing my experiences and shedding some light on what actually happened on the last day.

Today's Plan

Today promises to be a day of ~22 waking hours, if the day travelling from was to be anything like the day travelling to .

The days agenda will be a mix of sessions and labs (fingers crossed), all on the platform at INTERACT2008; then after the last lab or session a trip back to Blighty.

Who know's I might actually make it to the labs yet!

Unified Communications - Day 3

|
INTERACT2008BlogArt2 

Today promised to be a day of deep developer interaction on the platform at INTERACT2008. The day started with breakfast and then a keynote delivered by Terry Myerson the Corporate VP for . Followed by a day of developer code camps (kinda) and maybe there was a little time left for developer labs?

The Keynote

Today's keynote was very interesting, Terry described the biggest pain points for Microsoft Exchange over the last year or so and introduced the Exchange Labs, which is basically Exchange as a software service, implemented by some schools in the US. Exchange has never really featured in my career, other than as a user via Outlook etc. but it was interesting just the same, I like it.

The Sessions

Today was the for the day for all the developer sessions, by the end of it all I feel I really understand what APIs are available, what technologies and server roles are involved along with the best way to talk to each. In addition I now have a pretty good idea of Microsoft's strategy with regards to UC and the programmability of the platform. All good.

Developer Code Camps

There was some scheduling confusion with the code camp(s) and the was only really one code camp that was supposed to be after the sessions, but in the schedule there were many code camp sessions and they were running in parallel with the sessions, doh! But, once that was all ironed it turns out I could skip the actual code camp as they were just going through the code already shown during the sessions and in the labs. I felt that I had fully groked the sessions before the code camp and therefore didn't feel the need to see the instructor type the code in all over again. We were already running pretty late in the day too by this time, so my head was full and ready for bed.

The Labs

I didn't get to these today, but the sessions did cover most if not all of the content in them, as I have already said. I might take a look at these before I leave tomorrow just to be sure that I don't miss a trick.

Conclusion

Today was a good day at the INTERACT2008 conference on Microsoft Unified Communications, probably the best so far. Yesterday I got the "zen" of UC, today I saw what tools the developer has available and what is really possible. However, the bottom line for me is there is nothing here that will show me how to use the APIs that I might need to write about for my project (if we get the work that is), ho-hum.

Once again, it's time to bring out the same old sob story: it's very late for me (early hours of the morning back in my native GMT) so my bed calls. Night, night.

Titan - Files in the Cloud

|

[Update 2008-04-29: Derek has moved from Microsoft Live Spaces to Google Blogger, so I've updated the URL below.]

Derek is blogging again, after quite some break. He plans to talk about his new project, called Titan, which is written using WPF and .NET 3.5.

If you are interested in WPF in any way it will definitely be worth a look and keeping tabs on; I know that .

Unified Communications - Day 2

|
INTERACT2008BlogArt2 

After a nights sleep and some breakfast I was ready be to initiated into the Unified Communications fraternity, the day started with a keynote presentation, followed by a detailed IT Pro focused session, and then some labs (or so I thought).

The Keynote

The first event of the day, besides breakfast, was the keynote by Microsoft's , which was very illuminating. Gurdeep successfully got across the value proposition of unified communications to me, and he was host to a demonstration that really helped me understand where the platform sits in an enterprise and what it's capable of.

The Session

Today's sessions were really for the IT Pros. The only session I went to was one this morning on troubleshooting problems with the Office Communicator client. This session interested me because you can really gauge the maturity and the "reality" of a platform by the errors and associated fixes. I lost interest after about 30 minutes but I did get a good sense of the real issues people are facing and what solutions and tooling is available to help them.

The Labs

I figured that I'd spend the afternoon working through the developer labs. However, after the first few steps in the first lab is was clear to me that they were a little broken. I ended up spending the afternoon debugging and helping them to get their labs working. Maybe I'll find sometime either tomorrow or Thursday to actually do the labs myself, ho-hum.

Conclusion

Today was an interesting one that has given me a good understanding of the platform along with all the moving parts, as well as an excellent understanding of the sample app used in the labs (just not the UC bit of it... yet!) Tomorrow, I hope to get into the details of coding against the platform and maybe even have time for some labs.

Again, it's very late for me (early hours of the morning back in my native GMT) so my bed calls. Night, night.

Unified Communications - Day 1

|

I have arrived safely in San Diego; today was a day of travelling: London to LA to San Diego.

The journey was long but mostly uneventful; there was a 30 minute delay from LAX to San Diego, so no complaints.

It has been a 22 hour day so my bed calls, I will post more as the conference progresses. Night, night.

Bookshelf 2008

|

Last year I posted about what was on my book shelf and I've had enough interesting books pass through my hands since then that I thought I'd do a follow up post. Books of note on the self so far:

The first, Programming WPF, is the 2nd edition of the same book I spoke about in the previous bookshelf post; however - it is infinitely better, I cannot say enough good thinks about this book (see my , called Programming WPF is Perfect!, for more details). Many have said that you need to read 2 or 3 books on WPF to truly grok it; however, I believe you only need one: this one.

We stay with the WPF theme for the next book: 3D Programming for Windows that Charles kindly sent to me. I'm very pleased to say this is an excellent book. The topic of 3D programming in WPF is a little niche, but if this is what you're interested in then look no further, this book is excellent.

The next book is C# 3.0 in a Nutshell, I originally bought this a) because Chris Sells told me to and b) it was reported to have excellent coverage of LINQ, including XLINK and DLINK. What I found when I opened this book was that it had a lot more to teach me than just LINQ! For anyone who works with C# you need this book on your shelf, and you need to work as hard as you can to make a "dog eared" copy too! This book has replaced my recommendation for anyone wanting to learn C# and the .NET Framework. Brilliant work! Very well written.

Finally, this next book is easily as good as the others but it has one additional feature in its favour: the delivery of the content in this book is the best I have ever seen in a technical book! The book I'm referring to is by MLB of IDesign frame. This book does not only surpass her colleagues book , which in itself rocks, it cleanly wipes the floor with it, thanks Michele. I'm not going to say too much more about this book here as I want to do a post specifically about it.

I have read quite a few more books than just these few since my last bookshelf post but these are the cream of the crop. I hope you take the time to read all of them, I have no doubt that you'll enjoy them all.

So, what have you been reading?

INTERACT2008

|

I'm off to the INTERACT2008 conference next week, on a work assignment, this is an exclusive event for leaders in unified communications held April 8-10 in .

This inaugural technology event provides a special opportunity for you to develop deep technical knowledge about Microsoft’s unified communications products, build powerful new connections with leading professionals in the industry, and gain insights into the future of converging technologies.

One way to think about this technology is to think about the presence indicator in your "big boy" Outlook client; now think about how you would add that capability to your application. 

The product team at Microsoft have released a new Client API SDK for working with this stuff; you can get multitudinous amounts of information about Microsoft Unified Communications from the .

My new assignment requires that I learn the new SDK ASAP, so I'm off to . My focus will the .NET developer track, which I have yet to figure out from the gargantuan sized agenda, but it should be fun.

If you're going to the event or you're just in the area, then ping me via the . I'll post some more about the event upon my return.

If you're not going to be there where will you be?

Newer Posts Older Posts Home