Silvelright:ListBox无法用Tab顺序切换内部元素焦点的解决

时间:2022-04-23
本文章向大家介绍Silvelright:ListBox无法用Tab顺序切换内部元素焦点的解决,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

默认情况下,Silverlight自带的ListBox控件如果内部有多个TextBox,用户无法用键盘上的Tab键,在ListBox内部的TextBox之间切换。但Teterik RadControls 中的telerik:ListBox却很好的解决了这个问题,只要把telerik:ListBox的IsTabStop设置成false,同时把TabNavigation设置成Local即可(而SL自带的ListBox就算设置了这二个属性,Tab键需要按二次才能切换焦点)

完整Xaml代码:

<UserControl
		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
		xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
		xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" x:Class="List_Focus_Sample.MainPage"
		mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="35"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="35"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="35"/>
        </Grid.RowDefinitions>
    
        <TextBox Text="下面是Silverlight自带的ListBox" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        
        <ListBox Grid.Row="1"  IsTabStop="False" ItemsSource="{Binding ListCode, Mode=TwoWay}" BorderThickness="0" HorizontalAlignment="Center" VerticalAlignment="Center">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Text, Mode=TwoWay}" Width="150" Margin="5" BorderThickness="3"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" Margin="10"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <ContentPresenter Content="{TemplateBinding Content}"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
            <i:Interaction.Behaviors>
                <ei:MouseDragElementBehavior/>
            </i:Interaction.Behaviors>
        </ListBox>

        <TextBox Grid.Row="2" Text="下面是Telerik RadControls提供的ListBox" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="208,0,208,6"/>
        
        <telerik:ListBox  TabNavigation="Local" IsTabStop="False" Grid.Row="3" ItemsSource="{Binding ListCode, Mode=TwoWay}"  HorizontalAlignment="Center" VerticalAlignment="Center">
            <telerik:ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Text, Mode=TwoWay}" Width="150" Margin="5" BorderThickness="3"/>
                </DataTemplate>
            </telerik:ListBox.ItemTemplate>
            <telerik:ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" Margin="10"/>
                </ItemsPanelTemplate>
            </telerik:ListBox.ItemsPanel>
        
        	<i:Interaction.Behaviors>
        		<ei:MouseDragElementBehavior/>
        	</i:Interaction.Behaviors>
        </telerik:ListBox>

        <TextBox Text="Telerik又一次展示了它给力的一面" Grid.Row="4" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</UserControl>

 意外惊喜:之前写过一篇博文,讲述了 Silverlight自带的ListBox,无法应用Blend中的MouseDragElementBehavior(即:应用该行为仍然无法拖动ListBox),但是telerik:ListBox发现居然可以(本例中,用鼠标按住telerik:ListBox中的任一文本框的边框,即可拖动整个ListBox)--商业控件就是给力!