The trend for MVVM in Silverlight points to behaviors. I found this behavior online for a DoubleClick Trigger in XAML shown in Listing 1 written by Nigel Sampson. It's awesome. Basically the way it works is if the user selects the mousedown button, a timer is started. If the user clicks the mouse again before the timer interval is up, the actions in the trigger are invoked.
Listing 1 - Double Click Trigger Behavior for Silverlight
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Threading;
namespace Behaviors
{
public class DoubleClickTrigger : TriggerBase<UIElement>
{
{
{
Interval = new TimeSpan(0, 0, 0, 0, 200)
};
timer.Tick += OnTimerTick;
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.MouseLeftButtonDown += OnMouseButtonDown;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.MouseLeftButtonDown -= OnMouseButtonDown;
timer.Stop();
}
private void OnMouseButtonDown(object sender, MouseButtonEventArgs e)
{
if (!timer.IsEnabled)
{
timer.Start();
return;
}
timer.Stop();
InvokeActions(null);
private void OnTimerTick(object sender, EventArgs e)
{
timer.Stop();
}
}
Listing 2 - ListBox in XAML
<ListBox x:Name="SelectionListBox"
ScrollViewer.HorizontalScrollBarVisibility="Hidden" ItemsSource="{Binding Choices}"
ItemTemplate="{StaticResource ChoicesTemplate}" />
Our ChoicesTemplate will contain the trigger to implement the double click functionality. In this case we'll add the double click trigger directly on the textbox in our template. Notice that we also use MVVM Light's EventToCommand behavior to execute our details command after the user double clicks. You'll want to download MVVM Light to use this great behavior. I suppose you could have just as easily have used Blend's InvokeCommandAction. EventToCommand is just so much richer.
Listing 3 - ListBox ItemTemplate Containing DoubleClick Trigger
<DataTemplate x:Key="ChoicesTemplate">
<TextBlock Text="{Binding Description}" Foreground="White" >
<i:Interaction.Triggers>
<cx:DoubleClickTrigger>
<Command:EventToCommand Command="{Binding ShowDetailsCommand}" CommandParameter=
"{Binding ElementName=SelectionListBox}" />
</cx:DoubleClickTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>
Conclusion
Some mouse functionality that was easy in Windows and WPF, is not as obvious in Silverlight. The beauty about Silverlight is that you can create behaviors to react to almost any event you can trigger off of. DoubleClick is one such behavior that can be mimicked through some state managed code. You can hook the DoubleClick trigger on a textbox inside the item template and like magic, your listbox now has double click behavior on every element it displays. Now there's a template for success in Silverlight and C#.

shital pangarePosted Dec 29, 2011, 7:01 AM
Hi,Mike My Question is Suppose when i first click On button Visible some textbox But On next click I want Hide or collapsed that textBox So What is solution To Do this ?
Eyo EyoPosted Jun 18, 2011, 6:32 AM
Hi, When adding the double click directly to textblock it works nicely, however, if I try to use it as above from within a listbox it will not work. Please could you attach a sample project.
Mike GoldPosted Dec 8, 2010, 11:34 AM
If you have textboxes as the content of your datatemplate, they won't stretch across to fill the listbox. You'll need to do a little trick in Silverlight to make this happen. You can use the ItemsContainerStyle to tell the item to stretch. (putting Horizontal alignment stretch on the textbox won't work) Below is the solution:: Add this to your ListBox in XAML ItemContainerStyle="{StaticResource StretchItemAcross}" And create a style in your Resources: <Style x:Key="StretchItemAcross" TargetType="ListBoxItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style>