Thumb.Drag events are not raised in Silverlight™ custom control unless…

Ever tried to create a custom control for customizing the behavior of a Thumb object? It may not be as easy as expected. The first issue is that the Thumb class is sealed in Silverlight™ so there you cannot inherit your custom control from there. Instead you need to define a custom control and reuse a Thumb instance inside its template.

<ControlTemplate TargetType=”local:CustomThumb”>
   <Thumb x:Name=”PART_Thumb” Cursor=”Hand”>
     <Thumb.Template>…</Thumb.Template>
   </Thumb>
</ControlTemplate>

In code behind, of course, you find the actual Thumb object and attach DragStarted, DragDelta, and DragCompleted event handlers:

public override void OnApplyTemplate()
{
   …
thumb = GetTemplateChild(“Thumb”) as Thumb;
thumb.DragStarted += Thumb_DragStarted;
   …
}

The code compiles and builds just fine, but then you get a second issue: in Silverlight™ you’d soon find out that the event handlers are never raised, although you can confirm the Thumb is there! This is not required in WPF, but to resolve the issue in Silverlight™ you’d need to add a root panel to host the Thumb object!

<ControlTemplate TargetType=”local:CustomThumb”>
   <Grid>
     <Thumb x:Name=”PART_Thumb” Cursor=”Hand”>
       <Thumb.Template>…</Thumb.Template>
     </Thumb>
   </Grid>
</ControlTemplate>

I hope it would help somebody, someday…

About Sorin Dolha

My passion is software development, but I also like physics.
This entry was posted in Computers and Internet. Bookmark the permalink.

Add a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s