2011-08-12 16 views
1

Silverlightでは、上矢印または下矢印またはタブを押すと、現在ハイライト表示されている(選択されていない)項目の周りに小さなボックスが描画されます。私は、小さなボックスの周りにあるアイテムを見つけたいので、ユーザーがタブを押したときに選択したアイテムにすることができます。私はこれで3日間過ごしました。多分誰かがそれを使うことができます。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イベントを追加します(おそらくより簡単に行うよりも、言った)とキーがタブの場合は項目を選択し、イベントが集中している1に発射されます(その周りに箱を持っています) 、例えば私は多分あなたは何かを考えることができ、イベントを添付するためにいくつかのクリーンな方法を知っているでしょうWPFで

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

+0

+1興味深い考えです。 Louis Nardoziは実際に質問をしていませんでしたが、問題への自分自身の答えを示しています:) –

+0

@HiTechMagic:ああ、私はこれがいくつかの壊れたコードであると思っていました。彼自身も適切な答えとして掲示されているはずです。 –

関連する問題