Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 列表框+;WrapPanel箭头键导航_Wpf_Listbox_Wrappanel - Fatal编程技术网

Wpf 列表框+;WrapPanel箭头键导航

Wpf 列表框+;WrapPanel箭头键导航,wpf,listbox,wrappanel,Wpf,Listbox,Wrappanel,我试图实现WinFormsListView的等价物,其View属性设置为View.List。从视觉上看,以下方法很好。我的列表框中的文件名从上到下,然后换行到新列 以下是我正在使用的基本XAML: <ListBox Name="thelist" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Disabled"

我试图实现WinForms
ListView
的等价物,其
View
属性设置为
View.List
。从视觉上看,以下方法很好。我的
列表框中的文件名从上到下,然后换行到新列

以下是我正在使用的基本XAML:

<ListBox Name="thelist"
    IsSynchronizedWithCurrentItem="True"
    ItemsSource="{Binding}"
    ScrollViewer.VerticalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True"
                Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>
这会产生我想要的从最后一列到下一列中第一列的行为,但也会在左右箭头处理中产生一个奇怪的现象。任何时候使用向上/向下箭头从一列换行到下一列/上一列,随后使用向左或向右箭头键将所选内容移动到换行前所选项目的左侧或右侧

假设列表由字符串“0001”到“0100”填充,每列10个字符串。如果我使用向下箭头键从“0010”转到“0011”,然后按向右箭头键,选择移动到“0020”,就在“0010”的右侧。如果选择了“0011”,并且我使用向上箭头键将选择移动到“0010”,则按下右箭头键将选择移动到“0021”(在“0011”的右侧),按下左箭头键将选择移动到“0001”

如能帮助实现所需的列换行布局和箭头键导航,将不胜感激


(编辑移动到我自己的答案,因为从技术上讲,它是一个答案。)

结果是,当我处理
按键事件时,选择更改为正确的项目,但重点是旧项目

这是更新后的
KeyDown
eventhandler。由于绑定,
Items
集合返回我的实际项目,而不是
ListBoxItem
s,因此我必须在接近末尾时进行调用,以获取实际的
ListBoxItem
我需要调用
Focus()
on。通过交换调用
MoveCurrentToLast()
MoveCurrentToFirst()
可以实现从最后一项到第一项的换行,反之亦然


您应该能够在没有事件侦听器的情况下使用KeyboardNavigation.DirectionalNavigation,例如

<ListBox Name="thelist"
         IsSynchronizedWithCurrentItem="True"
         ItemsSource="{Binding}"
         ScrollViewer.VerticalScrollBarVisibility="Disabled"
         KeyboardNavigation.DirectionalNavigation="Cycle">

太好了,这对我帮助很大
private void thelist_KeyDown( object sender, KeyEventArgs e ) {
    if ( object.ReferenceEquals( sender, thelist ) ) {
        if ( thelist.Items.Count > 0 ) {
            switch ( e.Key ) {
                case Key.Down:
                    if ( !thelist.Items.MoveCurrentToNext() ) {
                        thelist.Items.MoveCurrentToLast();
                    }
                    break;

                case Key.Up:
                    if ( !thelist.Items.MoveCurrentToPrevious() ) {
                        thelist.Items.MoveCurrentToFirst();
                    }
                    break;

                default:
                    return;
            }

            e.Handled = true;
            ListBoxItem lbi = (ListBoxItem) thelist.ItemContainerGenerator.ContainerFromItem( thelist.SelectedItem );
            lbi.Focus();
        }
    }
}
<ListBox Name="thelist"
         IsSynchronizedWithCurrentItem="True"
         ItemsSource="{Binding}"
         ScrollViewer.VerticalScrollBarVisibility="Disabled"
         KeyboardNavigation.DirectionalNavigation="Cycle">