开发者

Find HIGHLIGHTED (NOT Selected) item in Combobox

In Silverlight, a little box is drawn around the currently HIGHLIGHTED (NOT Selected) item when you press up or down arrow or tab. I want to find out which item the little box is around so I can make it the selected item when the user presses tab. I spent three days on this, maybe someone else can use it.

       void SelectorRapidAccessKeyBehavior_DropDownOpened(object sender, EventArgs e)
        {
            FindPopup();
        }
        private void FindPopup()
        {
            CleanUpPopupHandler();
            pop = GetPopup(base.AssociatedObject);
     开发者_如何学JAVA       if (pop != null && pop.Child != null)
            {
                pop.Child.KeyDown += AssociatedObject_KeyUp;
                foreach (FrameworkElement c in Finder.FindVisualChildren<FrameworkElement>(pop.Child))
                {
                    c.KeyDown += new KeyEventHandler(c_KeyDown);
                }

            }
        }


        void c_KeyDown(object sender, KeyEventArgs e)
        {
            int t = this.AssociatedObject.TabIndex;
            Border ci = sender as Border;
            if (e.Key == Key.Tab)
            {
                if (ci != null)
                {


//this here is the magic line
                    var v = Finder.FindVisualChildren<FrameworkElement>((DependencyObject)pop.Child).Where(a => a.Opacity > 0 && a.Name == "FocusVisualElement" && a.Visibility == Visibility.Visible);//&& )
                    object o = v.First().DataContext;
                    int i = this.AssociatedObject.Items.IndexOf(o);
                    if (i > -1)
                        this.AssociatedObject.SelectedIndex = i;
                    pop.IsOpen = false;
                    DependencyObject d = Finder.FindParent<FloatableWindow>(this.AssociatedObject);
                    if (d == null)
                        d = Finder.FindParent<Window>(this.AssociatedObject);
                    Control c = Finder.FindVisualChildren<Control>(d).Where(a => a.TabIndex > t).OrderBy(a => a.TabIndex).FirstOrDefault();
                    if (c == null)
                        c = Finder.FindVisualChildren<Control>(d).OrderBy(a => a.TabIndex).FirstOrDefault();
                    if (c != null)
                        c.Focus();
                }
            }
        }


Just add a KeyDown event to the items (possibly easier said than done) and select the item if the Key is Tab, the event will be fired on the one that is focused (has the box around it), e.g.

<ComboBox Loaded="ComboBox_Loaded">
    <ComboBoxItem>1</ComboBoxItem>
    <ComboBoxItem>2</ComboBoxItem>
    <ComboBoxItem>3</ComboBoxItem>
    <ComboBoxItem>4</ComboBoxItem>
    <ComboBoxItem>5</ComboBoxItem>
</ComboBox>
private void ComboBoxItem_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Tab)
    {
        var cbi = sender as ComboBoxItem;
        var cb = cbi.Parent as ComboBox;
        cb.SelectedItem = cbi;
        e.Handled = true;
        cb.IsDropDownOpen = false;
    }
}

private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
    var cb = sender as ComboBox;
    foreach (var item in cb.Items)
    {
        (item as ComboBoxItem).KeyDown += ComboBoxItem_KeyDown;
    }
}

In WPF i would know of some cleaner ways to attach the events, maybe you can think of something.


  void SelectorRapidAccessKeyBehavior_DropDownOpened(object sender, EventArgs e)
    {
        FindPopup();
    }
    private void FindPopup()
    {
        CleanUpPopupHandler();
        pop = GetPopup(base.AssociatedObject);
        if (pop != null && pop.Child != null)
        {
            pop.Child.KeyDown += AssociatedObject_KeyUp;
            foreach (FrameworkElement c in Finder.FindVisualChildren<FrameworkElement>(pop.Child))
            {
                c.KeyDown += new KeyEventHandler(c_KeyDown);
            }

        }
    }


    void c_KeyDown(object sender, KeyEventArgs e)
    {
        int t = this.AssociatedObject.TabIndex;
        Border ci = sender as Border;
        if (e.Key == Key.Tab)
        {
            if (ci != null)
            {


//this here is the magic line
                var v = Finder.FindVisualChildren<FrameworkElement>((DependencyObject)pop.Child).Where(a => a.Opacity > 0 && a.Name == "FocusVisualElement" && a.Visibility == Visibility.Visible);//&& )
                object o = v.First().DataContext;
                int i = this.AssociatedObject.Items.IndexOf(o);
                if (i > -1)
                    this.AssociatedObject.SelectedIndex = i;
                pop.IsOpen = false;
                DependencyObject d = Finder.FindParent<FloatableWindow>(this.AssociatedObject);
                if (d == null)
                    d = Finder.FindParent<Window>(this.AssociatedObject);
                Control c = Finder.FindVisualChildren<Control>(d).Where(a => a.TabIndex > t).OrderBy(a => a.TabIndex).FirstOrDefault();
                if (c == null)
                    c = Finder.FindVisualChildren<Control>(d).OrderBy(a => a.TabIndex).FirstOrDefault();
                if (c != null)
                    c.Focus();
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜