This blog has, IMO, some great resources. Unfortunately, some of those resources are becoming less relevant. I'm still blogging, learning tech and helping others...please find me at my new home on http://www.jameschambers.com/.

Friday, May 29, 2009

Setting Template Properties on a ComboBox

In WPF the ComboBox is a tricky little control that doesn’t easily allow you to adjust properties on the inner controls of the template.

If you try to override the template you will find very quickly that you might get into the trap of having to override the entire visual tree, thus having to recreate the entire control from scratch.

Below is a way to get at the controls from the template, per my answer to a question on the MSDN forums. In particular, the question was about getting the TextBox (actually, in his case, the TextBlock) to have underlined text.

cboTest.Loaded += delegate {
if (cboTest.IsEditable)
{
TextBox textEditPart = ((TextBox)cboTest.Template.FindName("PART_EditableTextBox", cboTest));
textEditPart.TextDecorations = TextDecorations.Underline;
}
else
{
Grid layoutGrid = VisualTreeHelper.GetChild(cboTest, 0) as Grid;
ContentPresenter nonEditPart = VisualTreeHelper.GetChild(layoutGrid, 2) as ContentPresenter;
TextBlock textNonEditPart = VisualTreeHelper.GetChild(nonEditPart, 0) as TextBlock;
textNonEditPart.TextDecorations = TextDecorations.Underline;
}
};


Man, code on this blog is hard to read.  :o/



Okay, so what’s going on is basically that we have to hunt down the TextBox or TextBlock, depending on if the ComboBox is set to be editable or not.



After finding that, we simply set the TextDecorations property to the pre-defined Underline text decoration. Paste that code into your code-behind after InitializeComponent and you should be good to go.



I found the elements in question by using Mole.  When we have an object that is named, we can use the Template.FindName to get the control loaded up locally.  When we don’t, we’ll need to use VisualTreeHelper’s static GetChild method to dig.



Side note: this won’t work in a big way if you’ve otherwise edited your template.  This is fairly hard-coded against the VisualTree provided by ComboBox out-of-the-box (but could easily be adapted to be a little less tightly coupled).

Thursday, May 28, 2009

Sample Data Previews in Expression Blend 3

I am so glad there are people smarter than I am.  Especially folks that are working on Blend and who knew I would be posting about sample data, and how that might work. 

And the fact that they were working on it for months before I ever piped up. :oD

Expression Blend 3 does the exact thing that I was hoping Microsoft would integrate into the designers for WPF: sample data can now be auto-generated for you, or you can specify an XML file to populate the design-time data.

Schweet.

Here’s Soma’s preview from his blog…and an inexplicitly long set of gripes from people who obviously haven’t upgraded their IDE since Visual Interdev…

For those that are highly critical I encourage you to dive a little deeper than what's at the surface here. Templating, binding, providers, extensible components, the frameworks and base classes now provided, and now behaviours...there are a lot of great tools here.  I've been using .Net since 2001 and have deployed apps with up to 10,000 users.  It has come a long way, but you have to take off your "Here's what I know" cap and adopt the new functionality.

I remember when a friend got Vista, an upgrade from Win95.  She refused to start using the new document folders and insisted on using her old DOS-style directory structure for storing.  When she finally let go and adopted the new style of organization she was pleasantly surprised…

Visual Studio 2010 – WPF Designer

The new WPF designer is quite exciting, as are many of the features that are coming down the pike for us developers in Visual Studio 2010.

Karl Shifflett posted a link to the new designer intro videos on WindowsClient.net last night and I had a dive into them.  The coolest-ever-IT-d00d at my office is in the process of building me a Win7+VS2010 virtual machine as I write this. Yum.

Of note for me are the following:

  • The visual appeal isn’t quite my thing, but I’m getting to be an old guy and am actually a fan of the existing environment.  On the other hand, I love Expression Blend and its apparent ‘awareness’ of certain things, so I’m encouraged that Visual Studio 2010 seems to be moving in that direction.
  • The Data Source tool window has had some long-overdue features added to it, such as awareness of foreign-key relationships.
  • The draggy-droppability of the designer from the highly-aware Data Source tool window is ridiculous.  Before you get too critical of using tools to do those kinds of things (I have some buddies who just hate using code-generation tools and are hesitant to switch to .Net because of it), be sure you check out what’s really going on.  All of the binding you would do by hand is being done – elegantly, I might add – behind the scenes for you.  There is no locked-in overly-cumbersome data binding through properties that makes you rip out what little hair you have left…uh…oops.  Flashback to WinForms…
  • The WPF designer has had some really nice updates to it to handle things like layout for grids.  Again, this is a vehicle for code-generation of sorts, but the extra design-time help is great for keeping you in one place.  I am not a fan of having to ride three different work areas to accomplish a task, that being the designer, the properties tool window and the Xaml editor. This is a welcome change. You can now switch between column sizing modes on-the-fly from the editor, as an example, giving you richer control over the layout.
  • There are improvements as well to the workflow of adding and displaying images in projects.  While this wasn’t terrible before, how and where it handles and stores images is more streamlined and path syntax is created dynamically (as it was in 2008, but there is a UI for this now).
  •  imageMuch like the WPF designer in Visual Studio 2008, we have search functionality in the Properties/Events tool window.  This is especially important with the emerging model of binding, preview events and the like.  If you aren’t already using this, give it a try.  The screenie to the right is from 2k8.  The Properties tool window is quite enriched and better reveals data binding status and options.
  • The search functionality has been applied to the new resource selector, as well, allowing us to quickly filter through the dozens of brushes, paths, etc. that we may have in our project.  I have to admit that this is a little bit of validation for me.  My resource libraries have grown significantly over the last little while; that they have implemented search capabilities indicates that the way I style my app is common to the community ;o)
  • It seems that the Properties tool window is getting some much-needed love.  The in-line editors for properties are not so shallow.  A control’s font, for example, has a more Word-like property editor streamlined into the property editor.
  • Data binding during the design story has been quite enhanced.  Drilling into a composite type that the designer has bound for you allows you to drill into that type, select an appropriate converter and get away from MyObject.ThisNameSpace.TheClassIWrote being displayed in your label, for example.  This is part of that “awareness” I was talking about that makes the experience more fluid.
  • The tab page for the IDE is now akin to IE’s tabbing functionality.  While I do prefer this to the style of Visual Studio 2008, I am a greater fan of the Google Chrome approach, where hovering over a tab allows me to close it without having to bring the tab to the foreground.

There was a mention in one of the videos that multi-control selection and property setting would be in the release version of Visual Studio 2010 (so, you’d be able to select four controls and set a brush for the background).  I also hope that they consider a resource palette, so that I don’t have to drill into the properties window and a popup box to achieve the same result.

I’ve also wondered why there isn’t more made of the mouse.  We aren’t too low on the experience level here, as developers, so I think that more encompassing experiences should be worked into the developer experience through that simple rodent we keep on our desk.  Middle-click, anyone?  If I were working on Visual Studio 2010, I would have tried to create accelerators with the mouse.  For example, if I highlight a block of code and middle-click, bring up the refactor menu (instead of going through the right-click submenu).  Similarly, if I’m in the designer and right-click on a control, give me the context menu related to styling and applying resources.  Again, I like to stay in one place and have the in-context toolset at my fingertips.  Especially my middle finger.  Always proves handy.

I will come back with more when I get my hands a little more dirty with the 2010 beta.

Wednesday, May 27, 2009

Wordle-me, Baby

A good friend of mine sent a link to this great web site called Wordle that allows you to build word-images from a web page, a block of text, or any RSS/Atom capable blog or URL.

My blog looks like this:

image

So…apparently I talk about DATA, VISUAL and STYLE a fair bit.

It also looks like this:

image

And this:

image

…but all I see is a potato!?

A good read on the approach (at 30,000 feet) and a bunch of background are on the site. As a .Net developer I sigh a little when comments like this are made:

This would have been completely impossible without the Java platform. (source)

There are actually many ways to do this, .Net certainly not the only alternative to Java.  Any development platform that allows access to font metrics would allow for something similar to be developed.  I don’t mean to trivialize the work done here, nor to turn to a Java versus .Net debate, the point is that many languages and environments (Flash output from c++ cgi running on Apache, for example) could render similar - if not the same - results.

That said, this is very cool work from a bright mind.

VS.Net Crashes When Editing Xaml Resource File

I have found what I believe to be a bug in Visual Studio 2008 when editing a Xaml Resource file.

Visual Studio 2008 stops responding if you edit an in-use style to be something invalid.  Now, you may be thinking, “Why on Earth would you want your style to be invalid?” …and that’s not really where I’m going with this.

Below is a visual to get where I’m at.  Just, please, brace yourself before you set eyes on this image; I used up the remainder of my annual illustration budget to enhance and superimpose qualifiers to properly indicate the exact cause of the problem.  For inexperienced graphic observers, the following image may just cause your retinas to implode with visual excitement:

image

Pretty crazy graphics, eh?

Well, you’re still reading so your eyes must still be in tact.  Here’s what I was doing:

  1. Created a button style for use in a user control
  2. Moved the style into a resource file
  3. Applied the style to my button
  4. Wanted to change the margin as well, so I copied and pasted the ‘BorderThickness’ line. The only thing I like less that copy-and-pasting is typing…
  5. Visual Studio implodes (as your eyes may have from the previous illustration.

I’m assuming that the problem is that, internally, an exception is being kicked up somewhere because either the style can’t render (other invalid states cause Visual Studio to puke, too, like incomplete Xaml tags in the style), or because of a problem like I’ve illustrated with a property being set multiple times.

I did a little more digging and found that Visual Studio only stops responding if you have a designer open that uses the style.  In other words, if I close all the designers that have a button using the style in question, I am able to successfully copy and paste invalid code into the style.

The good news is that the work-around is easy: close all your designers before editing in-use styles.  The bad news is that, if you’re like me, you’re working in multiple designers at once and re-using styles.

I have five MVVM views implements as UserControls that compose my main UI (each spawning other views) so when you’re trying to pull everything together this can be a pain in the arse.


UPDATE: I did find this KB article that will allow for a hotfix download for qualified parties…calling in now for download instructions…

Tuesday, May 26, 2009

Out With the Old…

…and in with the new:

imageScreenie showing contact info, time in queue, 
customer feedback and technical rating of the caller.
 

I am really trying to follow some of the current trends in visualization.  What I’ve come to learn is that software users know what they want to do, they just don’t know how they want to do it.  What’s worse is that we’ve reinforced behaviours developed under poor software design and now many of us in the software development world are guilty of holding up those behaviours to point of encouraging them.

When a call comes into our helpdesk, the service reps don’t want to see all of the customer information; they want to see the information they need to confirm a customer’s identity.

The previous version of the software, which officially began development nearly a decade ago, wasn’t focused on a role or a task.  It was data-centric.  If you requested to see more data – because it would better serve your role or task – it was layered onto or composed into existing screens. 

The end result was that, over time, data started get lost.  Many bits of data were forgotten about, overlooked, under-used and – worse – re-implemented in different areas with slightly different meanings.

Using MVVM and WPF we are already improving the experience for the customer by improving the experience for the call staff.

I’m not saying I knocked it out of the park here, and arguably, someone with a little more design experience could improve this dramatically (go ahead! Write your own View and bind to my ViewModel!) but I think this is proof that software can look sexy and be functional at the same time.

Monday, May 25, 2009

Design Time Data in WPF

I came across an excellent article from Karl Shifflett that discussed a couple of different approaches for viewing data during a design time story.  There is nothing, by default, in Visual Studio that stubs out data for us so that we can see something in our template as we edit it.

image
The design-time and run-time experience, left-to-right.
 

Basically, if you’re creating templates for grids, listboxes or any other control in XAML you’re going to want to be able to see how that data looks without having to compile and run the app continuously.

By (heavily) using Karl’s project as a starting point, I have rebuilt his sample to a WPF-specific approach with imagesome suggestions on how to structure some of the project in a way that can grow a little better.

I also added a concrete example of how a design-time and run-time object could be set up for a little more clarity on how these objects play together.  These are found in the Data hierarchy in the project.

Also, as per Karl’s suggestion, I added a couple of data triggers to play with formatting in that way. There are triggers that reveal changes in either design time or run time (look for the comments).

Again, I borrowed heavily from Karl’s sample here, but wanted to make this sample a little more accessible to anyone who is a little newer to WPF.  There are still tons of opportunities to expand and explore this concept and to figure out how to make it work for you in your application development.

This is all cool, but it’s basically meaning more work and prep on any project you do.  I would love to see an extension to the designer that, in an intelligent way, would allow us to set a boolean value in the DataContext called, say, “EnableDesignTimeData”, that would then stub out as many instances of that class as we specify, perhaps in a “DesignTimeDataRowCount” property on the DataContext.  Allow the designer/support classes to build up rows based on this idea, but also allow us to drop an XML file (or build one through yet another extension to the designer, perhaps with pre-populated rows?) to use as the design-time data.  This would allow us to create data to cover all of the possible formatting scenarios that we may wish to express.

You can download the zip of the project here (VS.Net 2008).

When I get a chance this week, I also hope to MVVM-alize this sample and add a couple of commands.  That would be rockin-cool.