>
Blog
Book
Portfolio
Search

3/19/2009

3571 Views // 0 Comments // Not Rated

Weird Issue With Silverlight Repeat Buttons And Control Templates

I've always felt that one of the coolest features of WPF and Silverlight is that the "big" controls are simply composites of smaller ones. For example, in a button, you can dig in and find borders, gradients, etc.

Another one is the scroll viewer, which has a slider, buttons, and a rectangle. But these buttons are actually RepeatButtons, a special type of button that, while the left the mouse button is held down, continues to fire the click event at a specifiable interval.

For this Silverlight control I'm working on, I needed this behavior, but with a custom layout. To do this, I overwrote the RepeatButton's template with a control template, and provided my own visuals. At the root of my control template was a grid, which laid out the rest of the constituent controls.

Although it appeared to work, I noticed that the "repeat" behavior did not. In other words, it acted like a standard button. Well, it turns out that the root element of your RepeatButton's control template has be named "Root." The following XAML demonstrates what you have to do to get a templated RepeatButton to, well, repeat:

Code Listing 1

  1. <RepeatButton ... >
  2. <RepeatButton.Template>
  3. <ControlTemplate>
  4. <Grid x:Name="Root">
  5. ...
  6. </Grid>
  7. </ControlTemplate>
  8. </RepeatButton.Template>
  9. </RepeatButton>

If the Grid in Line #4 is named anything else, or not at all, the button won't repeat! I believe this is a known issue. I came across the fix here, and just wanted to pass it along. Have fun!

1 Tag

No Files

No Thoughts

Your Thoughts?

You need to login with Twitter to share a Thought on this post.


Loading...