【发布时间】:2016-05-19 14:10:35
【问题描述】:
A 有一个 ListBox,需要在某些情况下(例如显示搜索结果时)关注特定项目。这是通过附加属性完成的,使用以下函数:
public static void OnElementIndexPropertyChanged (DependencyObject DObject, DependencyPropertyChangedEventArgs Args)
{
ListBox ListBoxControl = DObject as ListBox;
if (ListBoxControl != null &&
(int)Args.NewValue != -1)
{
ListBoxControl.ScrollIntoView (ListBoxControl.Items[(int)Args.NewValue]);
ListBoxControl.UpdateLayout ();
var ListBoxItem = (ListBoxItem)ListBoxControl.ItemContainerGenerator.ContainerFromItem (ListBoxControl.Items[(int)Args.NewValue]);
ListBoxControl.SelectedItem = ListBoxItem;
ListBoxItem.Focus ();
}
}
现在,ListBoxControl 确实获得了键盘焦点(如果我按住 Alt,它将在其周围显示一个虚线边框,并且它使用箭头键向上/向下移动它可以正常工作),但是,样式没有更新。最后选定的项目以蓝色突出显示,而新选定的项目未更新以显示蓝色突出显示。它只在第一次正常工作,在项目被聚焦和突出显示之前。
除了手动更改背景/样式之外,还有其他方法吗?
【问题讨论】:
标签: c# wpf user-interface attached-properties