C# 将焦点从一个列表框移动到另一个列表框

C# 将焦点从一个列表框移动到另一个列表框,c#,wpf,xaml,listbox,C#,Wpf,Xaml,Listbox,我的输出类似于Windows8的开始菜单 以下是我的输出的屏幕截图: 通过从问题中获得帮助,我成功地获得了输出 XAML用于实现以下输出: <ItemsControl ItemsSource="{Binding MenuCategories}" > <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel IsItemsHost="True"

我的输出类似于Windows8的开始菜单

以下是我的输出的屏幕截图:

通过从问题中获得帮助,我成功地获得了输出

XAML用于实现以下输出:

<ItemsControl ItemsSource="{Binding MenuCategories}" >

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True" Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <TextBlock Text="{Binding Title}" FontSize="30" />

                <ListBox Grid.Row="1" x:Name="lst" ItemsSource="{Binding Design_Master_TileItem}" BorderThickness="0"
                         SelectedItem="{Binding DataContext.SelectedTile, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"
                         helpers:SingleSelectionGroup.SelectionGroup="Group">

                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel IsItemsHost="True" Orientation="Vertical" MaxHeight="{Binding ElementName=lst, Path=ActualHeight}"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>

                    <ListBox.Resources>
                        <Style TargetType="{x:Type ListBoxItem}">
                            <Setter Property="Width" Value="250" />
                            <Setter Property="Height" Value="125" />
                            <Setter Property="Margin" Value="2.5" />
                            <Setter Property="Padding" Value="2.5" />
                            <Setter Property="Background" Value="{Binding Background, Converter={StaticResource stringToBrushConverter}}" />
                            <Setter Property="Foreground" Value="White" />
                            <Setter Property="VerticalContentAlignment" Value="Bottom" />
                            <Style.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Foreground" Value="{Binding Background, Converter ={StaticResource stringToBrushConverter}}" />
                                </Trigger>
                                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                                    <Setter Property="IsSelected" Value="True"></Setter>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ListBox.Resources>

                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Height="125" Width="250">
                                <Path Data="{Binding ImageData}"  VerticalAlignment="Center" 
                                      Stretch="Uniform" Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
                                      Width="68" Height="68" Margin="10" RenderTransformOrigin="0.5,0.5">
                                    <Path.RenderTransform>
                                        <TransformGroup>
                                            <TransformGroup.Children>
                                                <RotateTransform Angle="0" />
                                                <ScaleTransform ScaleX="1" ScaleY="1" />
                                            </TransformGroup.Children>
                                        </TransformGroup>
                                    </Path.RenderTransform>
                                </Path>
                                <TextBlock Text="{Binding Title, Converter={StaticResource spaceToNewLineConverter}}" VerticalAlignment="Top" 
                                           Margin="40,10,10,10" FontSize="24" Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

                </ListBox>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

因此,从上面的代码中,您可能已经了解到Group和Ledger是listbox1的两个项目。另外四个是listbox2的项目

要求:

假设在listboxA中选择了ItemA1

案例1:新行为

如果ListBoxA在Item1的右侧没有任何项目,则当我按向右箭头键时,焦点应移到listboxB的ItemB1。同样,如果选择了listboxA的项目A2,则焦点应移到ListBoxB的项目B2

案例2:默认行为


如果ListBoxA在项目A1的右侧有一些项目,则应按向右箭头键选择该项目。我默认有这种行为,但我不想打扰它。我的意思是,在实现Case1时,我不想失去默认行为。

在各种项目中将焦点从一个列表框移动到另一个列表框需要复杂的逻辑。相反,您可以使用单个列表框,并根据标题进行分组。GroupStyle面板可以更改为水平方向的stackpanel,以实现您的UI。我就这样试过了,焦点也如期发挥作用

<ListBox ItemsSource="{Binding Source={StaticResource source}}" DisplayMemberPath="Name">
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </ListBox.GroupStyle>
    </ListBox>

您当前的xaml代码在这里运行良好。。有关详细信息,请参阅此链接

只需将键盘导航。DirectionalNavigation=“Continue”键盘导航。TabNavigation=“Continue”添加到列表框中,即可正常工作


...
..

您可以发布一个示例吗?因为我的输出中有一个空白列表框。请尝试此示例。[如果答案对您有帮助,请接受]我已对您的示例项目进行了一些更改。我已经上传了:但是我这里有一个小问题。如何减少同一组左右两侧两个项目之间的间距?我还想增加两个不同组之间的间距。ContainerStyle中ListBoxItem的边距将控制项目之间的间距。包装页边距将控制两组之间的间距。请接受答案。@Vishal您当前的xaml代码也正常工作,只是您需要添加KeyboardNavigation.DirectionalNavigation=“Continue”KeyboardNavigation.TabNavigation=“Continue”到listbox尝试一下这可能会节省您的工作,以备新用户使用code@HeenaPatil谢谢你的上述评论。我刚刚试过你在评论中提到的东西。但是我在使用你的代码时遇到了一些问题。当我按下箭头键时,它看起来工作正常,但是1。若选择了第一个列表框的第一项,那个么,若我按左箭头键,那个么列表框周围会有一个红色边框。2.如果选择了任何列表框的第一项并按下向上箭头键,则焦点将转到上面的控件,即菜单已聚焦。是的,现在它工作正常。但如果我不想骑自行车,那我该怎么办?我的意思是,如果选择了最后一项,并按下向下箭头键,则我不想将焦点移到第一项。@HeenaPatil明白了。我在ItemsControl上设置了KeyBoardNavigation.DirectionalNavigation=“Contained”,它可以按预期工作。请将以上评论作为答案发表,我将接受。@AnatoliyNikolaev请看一下Heena Patil提供的简单解决方案。它可能会在未来的项目中帮助你。谢谢希娜。根据stackoverfow.com的规定,我会在17小时后给你奖金。杰·马哈拉施特拉。不客气……杰·马哈拉施特拉!!
   <ItemsControl KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" Focusable="False"  >
        ...

        <ListBox Grid.Row="1" x:Name="lst" KeyboardNavigation.DirectionalNavigation="Continue" KeyboardNavigation.TabNavigation="Continue"   BorderThickness="0"/>
    ..

   </ItemsControl>