ListBox vs ItemsControl动态加载控件Tab键顺序

时间:2014-04-30 18:36:24

标签: wpf listbox tab-ordering

我对WPF中动态加载控件的Tab键顺序有疑问。

我有一个ListBox:

<ListBox ItemsSource="{Binding Path=MyData,Mode=TwoWay}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel></StackPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

我在stackpanel中添加了几个TextBlock-Textbox对(Name-Value对)作为ListItem。当我选中时,跳过所有跳过的项目,我从ListBox项目中退出。

解决方案:但是,如果我将以下内容添加到ListBox,一切正常:

KeyboardNavigation.TabNavigation="Local"

现在,如果我使用ItemsControl代替ListBox,我不必使用本地导航,它工作正常OOTB。我的ItemsControl看起来像这样:

<ItemsControl ItemsSource="{Binding Path=MyData,Mode=TwoWay}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel></StackPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

问题:为什么这两种行为不同?

谢谢, RDV

&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;完整的XAML代码&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; &LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;

<ItemsControl HorizontalContentAlignment="Center"
              HorizontalAlignment="Center"
              ItemsSource="{Binding Mydata}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
           <StackPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
               <Grid.ColumnDefinitions>
                 <ColumnDefinition/>
                 <ColumnDefinition/>
               </Grid.ColumnDefinitions>

               <Border Background="LightBlue">
                  <TextBlock Grid.Column="0"
                             HorizontalAlignment="Center" 
                             VerticalAlignment="Center"
                             Text="{Binding Name}"/>
               </Border>
               <TextBox  Grid.Column="1"
                         Text="{Binding Value}"
                         HorizontalAlignment="Stretch"
                         VerticalContentAlignment="Center"
                         HorizontalContentAlignment="Center"/>
             </Grid>
            </DataTemplate>
           </ItemsControl.ItemTemplate>
          </ItemsControl>

在ViewModel的MyData变量中添加一些数据。

只需将ItemsControl更改为ListBox即可查看Tab键行为的变化。如果您无法在ListBox中标记项目(我面临的问题),请添加到ListBox控件:

 KeyboardNavigation.TabNavigation="Local"

谢谢, RDV

0 个答案:

没有答案