C# 在组合框中查找突出显示(未选中)的项
在Silverlight中,当按向上或向下箭头或tab键时,会在当前高亮显示(未选定)的项目周围绘制一个小框。我想找出小框周围的项目,以便在用户按tab键时将其设置为选中项目。我花了三天时间在这上面,也许其他人可以用C# 在组合框中查找突出显示(未选中)的项,c#,silverlight,xaml,combobox,selection,C#,Silverlight,Xaml,Combobox,Selection,在Silverlight中,当按向上或向下箭头或tab键时,会在当前高亮显示(未选定)的项目周围绘制一个小框。我想找出小框周围的项目,以便在用户按tab键时将其设置为选中项目。我花了三天时间在这上面,也许其他人可以用 void SelectorRapidAccessKeyBehavior_DropDownOpened(object sender, EventArgs e) { FindPopup(); } pr
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();
}
}
}
void SelectorRapidAccessKeyBehavior\u下拉菜单打开(对象发送方,事件参数e)
{
FindPopup();
}
私有void FindPopup()
{
CleanUpPopupHandler();
pop=GetPopup(base.AssociatedObject);
if(pop!=null&&pop.Child!=null)
{
pop.Child.KeyDown+=关联对象\u keydup;
foreach(Finder.FindVisualChildren(pop.Child)中的框架元素c)
{
c、 KeyDown+=新的KeyEventHandler(c_KeyDown);
}
}
}
void c_KeyDown(对象发送方,KeyEventArgs e)
{
int t=this.AssociatedObject.TabIndex;
Border ci=发送方作为边框;
if(e.Key==Key.Tab)
{
如果(ci!=null)
{
//这是魔术线
var v=Finder.FindVisualChildren((DependencyObject)pop.Child)。其中(a=>a.Opacity>0&&a.Name==“FocusVisualElement”&&a.Visibility==Visibility.Visible);//&)
对象o=v.First().DataContext;
int i=this.AssociatedObject.Items.IndexOf(o);
如果(i>-1)
this.AssociatedObject.SelectedIndex=i;
pop.IsOpen=false;
DependencyObject d=Finder.FindParent(this.AssociatedObject);
如果(d==null)
d=Finder.FindParent(this.associated对象);
控件c=Finder.FindVisualChildren(d).Where(a=>a.TabIndex>t).OrderBy(a=>a.TabIndex).FirstOrDefault();
如果(c==null)
c=Finder.FindVisualChildren(d).OrderBy(a=>a.TabIndex.FirstOrDefault();
如果(c!=null)
c、 焦点();
}
}
}
只需在项目中添加一个按键事件(可能说起来容易做起来难),然后选择该项目。如果按键为Tab,则事件将在聚焦的项目上触发(框围绕着它),例如
1.
2.
3.
4.
5.
private void ComboBoxItem_KeyDown(对象发送方,KeyEventArgs e)
{
if(e.Key==Key.Tab)
{
var cbi=作为ComboBoxItem的发送方;
var cb=cbi.Parent作为组合框;
cb.SelectedItem=cbi;
e、 已处理=正确;
cb.IsDropDownOpen=错误;
}
}
已加载私有无效组合框(对象发送方,路由目标)
{
var cb=发送方作为组合框;
foreach(cb.Items中的var项目)
{
(项目作为ComboBoxItem).KeyDown+=ComboxItem\u KeyDown;
}
}
在WPF中,我知道一些更干净的附加事件的方法,也许你可以想出一些办法。void selector rapidaccesskeybehavior\u DropDownOpened(对象发送方,事件参数e)
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();
}
}
}
{
FindPopup();
}
私有void FindPopup()
{
CleanUpPopupHandler();
pop=GetPopup(base.AssociatedObject);
if(pop!=null&&pop.Child!=null)
{
pop.Child.KeyDown+=关联对象\u keydup;
foreach(Finder.FindVisualChildren(pop.Child)中的框架元素c)
{
c、 KeyDown+=新的KeyEventHandler(c_KeyDown);
}
}
}
void c_KeyDown(对象发送方,KeyEventArgs e)
{
int t=this.AssociatedObject.TabIndex;
Border ci=发送方作为边框;
if(e.Key==Key.Tab)
{
如果(ci!=null)
{
//这是魔术线
var v=Finder.FindVisualChildren((DependencyObject)pop.Child)。其中(a=>a.Opacity>0&&a.Name==“FocusVisualElement”&&a.Visibility==Visibility.Visible);//&)
对象o=v.First().DataContext;
int i=this.AssociatedObject.Items.IndexOf(o);
如果(i>-1)
this.AssociatedObject.SelectedIndex=i;
pop.IsOpen=false;
DependencyObject d=Finder.FindParent(this.AssociatedObject);
如果(d==null)
d=Finder.FindParent(this.associated对象);
控件c=Finder.FindVisualChildren(d).Where(a=>a.TabIndex>t).OrderBy(a=>a.TabIndex).FirstOrDefault();
如果(c==null)
c=Finder.FindVisualChildren(d).OrderBy(a=>a.TabIndex.FirstOrDefault();
如果(c!=null)
c、 焦点();
}
}
}
+1了解一个有趣的想法。Louis Nardozi并不是在问问题,而是展示了他自己对这个问题的答案:)@HiTechMagic:哦,我认为这只是一些坏代码,不管怎样,这里的问题仍然应该是问题,因此,可以有多个答案,他自己的答案也应该作为正确答案发布。请将您自己的解决方案作为正确答案发布,并将其从问题中删除,它不属于该问题。我已将您未注册的帐户合并到您已注册的帐户中,您现在拥有此问题的所有权,可以直接编辑它。