2011-08-12 57 views
1

在Silverlight中,当您按向上或向下箭头或选项卡时,在当前突出显示(未选定)项目周围绘制一个小框。我想知道小盒子在哪个项目周围,因此当用户按下标签时,我可以将其选定为所选项目。我花了三天时间,也许有人可以使用它。在Combobox中查找突出显示(未选中)项目

 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

请发表您自己的解决方案作为正确的答案,并将其从问题中删除,它不属于那里。 –

+0

我已将您的未注册帐户合并到您的注册帐户中,您现在拥有此问题的所有权,并且可以直接对其进行编辑。 –

+0

谢谢Tim的编辑! –

回答

0
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(); 
      } 
     } 
    } 
1

只是一个KeyDown事件添加到项目(比做可能说起来容易),选择项目,如果关键是标签,该事件将体现在集中的一个被解雇(周围有框) ,例如

<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; 
    } 
} 

在WPF我会知道的一些简洁的方式来连接的事件,也许你能想到的东西。

+0

+1有趣的想法。路易斯Nardozi实际上并没有问一个问题,但显示他自己的问题的答案:) –

+0

@HiTechMagic:哦,我认为这只是一些破碎的代码,无论哪种方式问题在这里应该仍然是问题,因为可以有多个答案他自己也应该作为正确答案发布。 –

相关问题