(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

Keyboard selection on Silverlight ListBox and ComboBox

(0 votes)
8 comments   /   posted by Silverlight Show on Feb 09, 2010
Tags:   Listbox , ComboBox
Fons Sonnemans demonstrates a behavior that allows keyboard selection on a ListBox or ComboBox.

Silverlight doesn't support keyboard selection on a ListBox or Combox. I have created a small Behavior which fixes this problem. You can attach the KeyboardSelectionBehavior to a ListBox or ComboBox using Microsoft Expression Blend. You drag it from the Assets and drop it on your ComboBox or ListBox. If you have a custom ItemTemplate you will have to set the SelectionMemberPath property.


Comments

  • -_-

    RE: Keyboard selection on Silverlight ListBox and ComboBox


    posted by m.k.devadiya on Jul 19, 2010 15:54

    might be this will help you....

    www.codeproject.com/.../silverlight/ComboBoxKeyBrdSelection.aspx

  • -_-

    RE: Keyboard selection on Silverlight ListBox and ComboBox


    posted by joe brockhaus on Dec 23, 2010 19:16

    RE: 'might be this will help you..'

    that project, if i may say so, is an ugly hack.

    You should use a behavior like Fons does to implement this ... behavior.

    I've extended Fons's implementation to maintain a history of keypresses, and a new public property for the developer to set a timeout period (ms) for the reset of this string.

    (Consider a list with 26 items that start with "A". Items range from "Aa.." to "Az.." The user would likely wish to be able to set focus on the control, type "A-Y" [within the configured timeout period] and arrive at item Ay with 2 keypresses, as opposed to 25 keypresses of the letter "A".)

  • -_-

    RE: Keyboard selection on Silverlight ListBox and ComboBox


    posted by joe brockhaus on Dec 23, 2010 23:32

    new properties:

    private string keyDownHistory;
    private DateTime _lastKeyDownTime;
    private DateTime LastKeyDownTime
    {
        get
        {
            if (this._lastKeyDownTime == null)
                this._lastKeyDownTime = DateTime.Now;
            return this._lastKeyDownTime;
        }
        set { _lastKeyDownTime = value; }
    }
    /// <summary>
    /// Gets or sets the Timeout (ms) used to reset the KeyDownHistory item search string
    /// </summary>
    public double KeyDownTimeout { get; set; }


    Edits to DoKeyDown(object sender, KeyEventArgs e)

    //
    #region Edits, just below the creation of the 'list' of values
    //
                // calculate how long has passed since the user typed a letter
                var elapsedTime = DateTime.Now - this.LastKeyDownTime;
                if (elapsedTime.TotalMilliseconds <= this.KeyDownTimeout)
                { /* if it's less than the timeout, add to the search string */
                    this.keyDownHistory += e.Key.ToString();
                }
                else
                { /* otherwise replace it */
                    this.keyDownHistory = e.Key.ToString();
                }
    //
    #region existing code, sans change of searchText var set == keyDownHistory
    //
                // Find first starting with the search string            
                var searchText = this.keyDownHistory;
                var first = list.FirstOrDefault(item => item.Text.StartsWith(searchText,
                    StringComparison.InvariantCultureIgnoreCase));
                if (first != null)
                {
                    // found
                    this.AssociatedObject.SelectedIndex = first.Index;
                }
    //
    #endregion
    //
                // reset the last time a key was pressed
                this.LastKeyDownTime = DateTime.Now;
    //
    #endregion
  • -_-

    RE: Keyboard selection on Silverlight ListBox and ComboBox


    posted by joe brockhaus on Dec 23, 2010 23:42

    also, an else block should most likely be added to the if (first != null) statement.

    if the user types successive characters which result in a null search through the items, he/she should not have to wait for the timeout to elapse before the search string is reset - it should be reset if the search returned a null.

    solution: reset the lastKeyDownTime & set keyDownHistory = string.Empty

  • -_-

    RE: Keyboard selection on Silverlight ListBox and ComboBox


    posted by Rabindra on Jan 20, 2011 16:12
    With this , Fons Sonnemans sample, if i try to group select(from Searched item to new item through Mouse) by holding Shift Key , it is selecting the whole bunch of item rather than selcting the last selected to newly selected.
  • -_-

    RE: Keyboard selection on Silverlight ListBox and ComboBox


    posted by rabindra on Jan 24, 2011 07:57
    With this , Fons Sonnemans sample, if i try to group select(from Searched item to new item through Mouse) by holding Shift Key , it is selecting the whole bunch of item rather than selcting the last selected to newly selected.
    Can any one help me to resolve this issue?
  • m.bihler

    Re: Keyboard selection on Silverlight ListBox and ComboBox


    posted by m.bihler on Aug 03, 2011 21:56

    The extension of joe brockhaus is a really good!
    There is only one little catch: It only works for letters and not if there are any numeric characters in the list items.

    To get this work, just change the search string calculation to:

     

     

        if (elapsedTime.TotalMilliseconds <= this.KeyDownTimeout)
        {
            // if it's less than the timeout, add to the search string
            this.keyDownHistory += (char)e.PlatformKeyCode;
        }
        else
        {
           
    // otherwise replace it
           
    this.keyDownHistory = ((char)e.PlatformKeyCode).ToString();
        }

  • SlavaPankov

    Re: Keyboard selection on Silverlight ListBox and ComboBox


    posted by SlavaPankov on Jan 12, 2012 01:07

    Instead of PlatformKeyCode it's better to use the following:

    string key = e.Key.ToString();
                if (key.Length > 1 && (key.StartsWith("D") && char.IsDigit(key[1]) || key.StartsWith("NumPad")))
                { //remove the D/NumPad prefix to get the digit
                  key = key.Replace("NumPad", "").Replace("D", "");
                }
                else if (key.Length > 1) { // unexpected special key, stop searching
                  this.keyDownHistory = string.Empty;
                  this.LastKeyDownTime = DateTime.Now;
                  return;
                }

Add Comment

Login to comment:
  *      *       
Login with Facebook

Our News on Twitter