Light Up the Web

Blog about programming in Silverlight

  Home  |   Contact  |   Syndication    |   Login
  24 Posts | 0 Stories | 45 Comments | 0 Trackbacks

News

My post about Deep Zoom Composer was recommended by Scott Guthrie! :) (see here)

Archives

About Me

News

Who was here

Saturday, October 03, 2009 #

After my last successes with TextBox Button Inovker Behavior, which was downloaded more than 700 times in 7 days and recommended by Silverlight sites like http://silverlike.net/invoke-button-click-event/, http://silverlightcream.com/, http://geekswithblogs.net/WynApseTechnicalMusings/ ,  I got motivation to share with you one more. This time my behavior will not be so useful and probably won’t be so famous but I hope It will be helpful for someone.

What I did

What I did this time is a simple behavior for opening ComboBox when mouse is over it. It might be useful in some scenario and makes one more click less for user. You can also find similar solution on youtube.com site where you don’t have to click ComboBox, but only move mouse over user's profile.

Live demo

How to add this to application

<ComboBox>
 
<interaction:Interaction.Behaviors>
     
<behavior:OpenComboBoxBehavior />
 
</interaction:Interaction.Behaviors>

<!-- sample data -->
 
<ComboBoxItem>
     
<TextBlock Text="First element" />
 
</ComboBoxItem>
 
<ComboBoxItem>
     
<TextBlock Text="Second element" />
 
</ComboBoxItem>
 
<ComboBoxItem>
     
<TextBlock Text="Third element" />
 
</ComboBoxItem>
 
<ComboBoxItem>
     
<TextBlock Text="Fourth element" />
 
</ComboBoxItem>
</ComboBox>
where "interaction:" is a namespace for "clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" and "behavior:" is a namespace for behavior's code.

Source Code

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

/// Behavior for ComboBox control.
/// It invokes drop down list when mouse is over it.
///
/// Jacek Ciereszko
/// http://geekswithblogs.net/SilverBlog/
///

///

namespace ComboBoxBehavior
{
   
public class OpenComboBoxBehavior : Behavior
   
{
       
///
       
/// Called after the Behavior is attached to an AssociatedObject.
       
///

       
/// Override this to hook up functionality to the AssociatedObject.
       
protected override void OnAttached()
       
{
           
base.OnAttached();
           
this.AssociatedObject.MouseEnter += new MouseEventHandler(AssociatedObject_MouseEnter);
       
}

       
///
       
/// Called after the Behavior is detached from an AssociatedObject.
       
///

       
/// Override this to hook up functionality to the AssociatedObject.
       
protected override void OnDetaching()
       
{
           
this.AssociatedObject.MouseEnter -= new MouseEventHandler(AssociatedObject_MouseEnter);
           
base.OnDetaching();
       
}

       
///
       
/// When mouse is over ComboBox, control drop down will open
       
///

       
///
       
///
       
void AssociatedObject_MouseEnter(object sender, MouseEventArgs e)
       
{
           
this.AssociatedObject.IsDropDownOpen = true;
       
}
   
}
}

Download

Source code and live example is also available on http://gallery.expression.microsoft.com/en-us/OpenComboBoxDropDown

---
Jacek Ciereszko

Polish version: http://jacekciereszko.pl/2009/10/behaviors-mouse-over-combobox-open.html
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati