如何在NumericUpDown控件Silverlight中设置TextBox的TabIndex

时间:2011-08-16 07:44:55

标签: c# .net wpf silverlight numericupdown

我在Silverlight应用程序中使用NumericUpDown控件。使用tab浏览Silverlight页面中的控件时出现问题。

案例1:当我的控制焦点在NumericUpDown上并且我使用向下键时,我的值在NumericUpDown控件中递增和递减。当按下标签焦点移动到下一个控制谁的tabindex是+1

案例2 当我点击NumericUpDown控件时,我的NumericUpDownControl会因为点击它而聚焦,当我通过键盘输入值并按下tab时,id不会移动到下一个控件。 (因为它在第一种情况下移动)

我发现当我点击NumericUpDown控件然后在案例2中按Tab键时:在控制键的关键事件中,我将原始源文件作为TextBox。

**   private void Control_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.OriginalSource as Control != null
            && e.Key == Key.Tab)
        {
            Int32 index = (e.OriginalSource as Control).TabIndex;
            if (index == Int32.MaxValue) index = index1; //??//
            if (Keyboard.Modifiers == ModifierKeys.None)
            {

            }
            else if (Keyboard.Modifiers == ModifierKeys.Shift)
            {

            }
        }
    }

**

案例1 :我将NumericUpDown控件作为原始源 案例2 :我将TextBox控件作为原始源

我已经设置了NumericUpDown控件的TabIndex,因为我没有获得TextBox的TabIndex属性,所以我遇到了上述问题。

那么如何在NumericUpDown控件的TextBox上设置TabIndex。??

控制模板

`                                                                 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

                            <VisualStateGroup x:Name="ValidationStates">
                                <VisualState x:Name="Valid"/>
                                <VisualState x:Name="InvalidUnfocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="InvalidFocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsOpen">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <sys:Boolean>True</sys:Boolean>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>

                        </VisualStateManager.VisualStateGroups>
                        <inputToolkit:ButtonSpinner x:Name="Spinner" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" MinWidth="35">
                            <StackPanel Background="White" Orientation="Horizontal" HorizontalAlignment="Right" Width="Auto">                                   
                                <TextBox x:Name="Text"  Style="{StaticResource TextBoxStyle}"  
                                     BorderThickness="0" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" FontStyle="{TemplateBinding FontStyle}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" MinWidth="50" Width="Auto"  AcceptsReturn="False" Text="{TemplateBinding Value}" TextAlignment="Left" TextWrapping="NoWrap"/>
                            </StackPanel>
                        </inputToolkit:ButtonSpinner>
                        <Border x:Name="DisabledVisualElement" IsHitTestVisible="false" Opacity="0" Background="#A5FFFFFF" CornerRadius="2.5,2.5,2.5,2.5"/>
                        <Border x:Name="FocusVisualElement" IsHitTestVisible="False" Opacity="0" BorderBrush="#FF45D6FA" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1,1,1,1"/>
                        <Border x:Name="ValidationErrorElement" Visibility="Collapsed" BorderBrush="#FFDB000C" BorderThickness="1" CornerRadius="1">
                            <ToolTipService.ToolTip>
                                <ToolTip x:Name="validationTooltip" Height="Auto" Width="Auto" Template="{StaticResource ValidationToolTipTemplate}" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                                    <ToolTip.Triggers>
                                        <EventTrigger RoutedEvent="Canvas.Loaded">
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsHitTestVisible">
                                                        <DiscreteObjectKeyFrame KeyTime="0">
                                                            <DiscreteObjectKeyFrame.Value>
                                                                <sys:Boolean>true</sys:Boolean>
                                                            </DiscreteObjectKeyFrame.Value>
                                                        </DiscreteObjectKeyFrame>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                    </ToolTip.Triggers>
                                </ToolTip>
                            </ToolTipService.ToolTip>
                            <Grid Height="12" HorizontalAlignment="Right" Margin="1,-4,-4,0" VerticalAlignment="Top" Width="12" Background="Transparent">
                                <Path Fill="#FFDC000C" Margin="1,3,0,0" Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z"/>
                                <Path Fill="#ffffff" Margin="1,3,0,0" Data="M 0,0 L2,0 L 8,6 L8,8"/>
                            </Grid>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>`

**我想在运行时从<inputToolkit:ButtonSpinner></inputToolkit:ButtonSpinner> Text的TabIndex属性设置TextBox。

我该怎么做??**

1 个答案:

答案 0 :(得分:0)

<TextBox x:Name="Text"  Style="{StaticResource TextBoxStyle}"  
                                 BorderThickness="0" FontFamily="{TemplateBinding FontFamily}" TabIndex="{TemplateBinding TabIndex} FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" FontStyle="{TemplateBinding FontStyle}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" MinWidth="50" Width="Auto"  AcceptsReturn="False" Text="{TemplateBinding Value}" TextAlignment="Left" TextWrapping="NoWrap"/>  

设置TabIndex =“{TemplateBinding TabIndex}为我工作