Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Friday, November 16, 2007

WPF Routed Commands and Focus Scope

Some WPF concepts are not as simple as it might look like. Case in point: Routed Commands. I created the following XAML snippet, and expected the buttons to function out-of-box:

<StackPanel>
    <TextBox/>
    <Button Content="Copy" Command="ApplicationCommands.Copy"/>
    <Button Content="Cut" Command="ApplicationCommands.Cut"/>
    <Button Content="Paste" Command="ApplicationCommands.Paste"/>
 </StackPanel>
It didn't. After a bit of googling, I found the solution here. I.E. The TextBox and Buttons must be put under different containers and the container for the buttons must have FocusManager.IsFocusScope set to true:
<StackPanel>
    <TextBox/>
</StackPanel>
<StackPanel FocusManager.IsFocusScope="True">
    <Button Content="Copy" Command="ApplicationCommands.Copy"/>
    <Button Content="Cut" Command="ApplicationCommands.Cut"/>
    <Button Content="Paste" Command="ApplicationCommands.Paste"/>
</StackPanel>
The post, however, didn't fully explain the connection between Focus Scope and Routed Command. As another bizarre example, when you set the same property on the first StackPanel that contains the TextBox, the buttons fail again.
I couldn't find any official documentation on this behavior. But did manage to dig up this forum reply (the last post on the page) that explains it fairly well. Here is my understanding:
  • By default, Routed Command travels from the source element up the visual tree, from child to parent. In our first example, this involves the button clicked and StackPanel. The text box that contains the command binding never gets a chance to handle the command, hence the buttons were never effective.
  • Setting IsFocusScope creates a separated focus scope that tracks its own focus element. In this case, the routing of command takes a detour: it first bubbles from the button clicked to the StackPanel, which now acts as the root of the newly created focus scope, then it jumps to the focus element of the default visual tree and travel up to its root. Because the focus element in the default visual tree is the text box, it now gets a chance to handle the command.
  • When the stack panel containing the text box is defined as another focus scope, it effectively moves the text box out of the command routing path (because it's no longer in the default visual tree focus scope). Hence the buttons seize to function again. 

Thursday, October 11, 2007

WPF: Interactive 2D Controls in 3D space

The power of WPF really has no limits. With just a little help, you can place 2D controls (text box, button, you name it...) on to 3D space. The coolest of all, these controls are fully operational!

The source code download contains a couple examples in XAML as good starting points.

What's under the hood.

Monday, October 8, 2007

Notes on the infrastructure of WPF

A Four hour train ride to Boston is the pefect timing to do some catch up reading on WPF, and there had been a few thoughts popping up somewhere in Conneticut and Rhode Islands. Some notes to check on later:

  • The entire Dependency Properties, Attached Properties and Routed Events framework is great. But what does it take to move these components out of the WPF framework and into your application? How much do they depend on WPF (UIElements, etc)? I know this is not easy, as WF has got a separate system for it.
  • As the same time, this framework is actually bringing C#/.Net many steps closer to a scripting language (I.E. objects are no more than a group of loosely typed hashtables). Is there any real disadvantage to this approach, aside from performance?
  • The Automation Peers are more than a natural way to perform unit tests on GUI components. Are there any related products/frameworks available from Microsoft or someone else?

Friday, May 18, 2007

WPF Samples

Check out this WPF Sample package by Kevin Moore.

  • The MineSweeper is an excellent example of custom FrameworkElement.
  • The Graph sample animates cool…
  • Be sure to check out the KevinButton (and click it).

Monday, May 14, 2007

What's Cool in WPF

Some of the new controls in Windows Presentation Foundation are, in the lack of a better word, brilliant! For a starter, look at the xamCarouselPanel in Infragistics NetAdvantage package. Here is a screen shot of a WPF front end I’m working on for Linq query engine:
 

In case you have been living under the rock for the past two years and has never heard WPF or XAML before, Josh Smith’s five part guided tour serves as a fantastic starting point.