我的情况如下:
<ListView ItemsSource={Binding TestList}>
<ListView.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Key, BindingMode=OneWay}">
<Expander.Content>
<ListView ItemsSource={Binding Value}>
<ListView.ItemContainerStyle>
<Style TargetType = {ListViewItem}>
<Setter Property = "IsSelected" Value={Binding IsSelected}/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Expander.Content>
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
测试清单:
Dictionary<string,ObservableCollection<Test>> TestList;
测试:
class Test : NotificationObject
{
public string Name { get; set;}
private bool isSelected;
public bool IsSelected
{
if( value != isSelected )
{
isSelected = value;
RaisePropertyChanged("IsSelected");
}
}
}
内部的ListView SelectionMode是Single,我可以使用键盘上和下按钮浏览ListView中的项目。有没有一种方法可以实现此方案:当焦点位于ListView的最后一项上时,请按向下键,焦点将位于下一个ListView的第一项上;如果焦点位于下一个ListView的最后一项上,则将焦点移到下一个ListView。当焦点位于ListView的第一项上时,按向上键,焦点位于上一个ListView的最后一项。
有人可以帮忙吗?
解决方案
public static List<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
List<T> list = new List<T>();
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
list.Add((T)child);
}
List<T> childItems = FindVisualChildren<T>(child);
if (childItems != null && childItems.Count() > 0)
{
foreach (var item in childItems)
{
list.Add(item);
}
}
}
}
return list;
}
private void ListView_PreviewKeyDown(object sender, KeyEventArgs e)
{
ListView currentListView = sender as ListView;
List<ListView> listviews = FindVisualChildren<ListView>(ModulesListView);
int currentListViewIndex = listviews.IndexOf(currentListView);
// If the press key is Down and the selected item is the last one
if (Keyboard.IsKeyDown(Key.Down)&& currentListView.SelectedIndex + 1 == currentListView.Items.Count)
{
if (currentListViewIndex + 1 < listviews.Count)
{
// Get next ListView
var nextListView = listviews.ElementAt(currentListViewIndex + 1);
if (nextListView.Items != null && nextListView.Items.Count > 0)
{
ListViewItem item = nextListView.ItemContainerGenerator.ContainerFromIndex(0) as ListViewItem;
item.Focus();
item.IsSelected = true;
e.Handled = true;
}
}
}
else if (Keyboard.IsKeyDown(Key.Up)&& currentListView.SelectedIndex == 0)
{
if (currentListViewIndex > 0)
{
var previousListView = listviews.ElementAt(currentListViewIndex - 1);
if (previousListView.Items != null && previousListView.Items.Count > 0)
{
ListViewItem item = previousListView.ItemContainerGenerator.ContainerFromIndex(previousListView.Items.Count - 1) as ListViewItem;
item.Focus();
item.IsSelected = true;
e.Handled = true;
}
}
}
}
这个问题已经解决了!我已将解决方案粘贴到问题下方,希望它可以帮助有相同需求的其他成员,谢谢!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句