有关TabNavigation的方式【项目】
问题
外层MainWindow.xaml里有一个Container(ContentPresenter),调用一个CreateJob 页面,里面是一行行的<TextBlock>,最后一个是<DataPicker>。
因为会调用不同的子页面,这个Containner(ContentPresenter)需要写清TabNavigation的方式,这里是Local
<ContentPresenter KeyboardNavigation.TabNavigation="Local" Grid.Row="2" Content="{Binding CurrentViewModel}">
而里面这些Textblock设置了一定顺序的Index,最后一个是<DataPicker>的index是最大的。
但是这个datapicker是由一个DataTemplate组成的,其再内层是一个TextBlock和一个Button组成。
问题是KeyboardTab不会走到内层的DataPicker的Button中。
解决
1:检查内层DataPicker的DataTemplate的Button有没有设置Focusable= false
2:外层<DataPicker>要是KeyboardNavigation.TabNavigation="Continue"
<DatePicker Grid.Row="5" Grid.Column="1" Margin="10,5,0,5" Focusable="True" KeyboardNavigation.TabIndex="11" KeyboardNavigation.TabNavigation="Continue" Style="{StaticResource Base_DatePicker_Template}" Text="{Binding CurrentJob.RevisionDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" />
给出CreateJob页面空间的内部完整设置:
<TextBox Grid.Column="1" MaxLength="64" Margin="10,5,0,5" KeyboardNavigation.TabIndex="0" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.Client, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Grid.Row="1" Margin="10,5,0,5" HorizontalAlignment="Right" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" Text="Field/Lease" /> <TextBox Grid.Row="1" Grid.Column="1" MaxLength="64" Margin="10,5,0,5" KeyboardNavigation.TabIndex="1" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.FieldLease, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Grid.Row="2" Margin="10,5,0,5" HorizontalAlignment="Right" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" Text="Well Name" /> <TextBox Grid.Row="2" Grid.Column="1" MaxLength="40" Margin="10,5,0,5" KeyboardNavigation.TabIndex="2" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.WellName, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Grid.Row="3" Margin="10,5,0,5" HorizontalAlignment="Right" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" Text="Company Rep" /> <TextBox Grid.Row="3" Grid.Column="1" MaxLength="64" Margin="10,5,0,5" KeyboardNavigation.TabIndex="3" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.CompanyRep, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock x:Name="AreaCountryLabel" Grid.Row="4" Margin="10,5,0,5" HorizontalAlignment="Right" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" Text="Rig Name" /> <TextBox Grid.Row="4" Grid.Column="1" MaxLength="64" Margin="10,5,0,5" KeyboardNavigation.TabIndex="4" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.RigName, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Grid.Row="5" Margin="10,5,0,5" HorizontalAlignment="Right" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" Text="Rig Type" /> <TextBox Grid.Row="5" Grid.Column="1" MaxLength="32" Margin="9.999,6.067,0,6.067" KeyboardNavigation.TabIndex="5" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.RigType, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Grid.Row="6" Margin="0,5" HorizontalAlignment="Right" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" Text="Rig Phone Number" Width="137.605" /> <TextBox Grid.Row="6" Grid.Column="1" MaxLength="32" Margin="9.999,6.067,0,6.067" KeyboardNavigation.TabIndex="6" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.RigPhoneNumber, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Grid.Row="7" Margin="0,5" HorizontalAlignment="Right" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" Text="Casing Size"/> <TextBox Grid.Row="7" Grid.Column="1" MaxLength="32" Margin="9.999,6.067,0,6.067" KeyboardNavigation.TabIndex="7" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.CasingSize, UpdateSourceTrigger=PropertyChanged}" /> </Grid> <Grid VerticalAlignment="Top" Margin="0,20,60,0" d:LayoutOverrides="Width" Grid.Column="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.3*" /> <ColumnDefinition Width="0.7*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock HorizontalAlignment="Right" Margin="10,5,0,5" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" Text="Programmed By" /> <TextBox Grid.Row="0" Grid.Column="1" MaxLength="32" Margin="10,5,0,5" KeyboardNavigation.TabIndex="8" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.ProgrammedBy, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Grid.Row="1" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" HorizontalAlignment="Right" Margin="10,10,0,5" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" /> <TextBlock Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Center" FontSize="18" Margin="10,5,0,5" Style="{StaticResource Univers57_TextBlock_Style}" Text="Sales Order No." /> <TextBox Grid.Row="2" Grid.Column="1" MaxLength="32" Margin="10,5,0,5" KeyboardNavigation.TabIndex="9" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.SalesOrderNo, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Grid.Row="3" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" HorizontalAlignment="Right" Margin="10,10,0,5" /> <TextBlock Grid.Row="4" HorizontalAlignment="Right" VerticalAlignment="Center" FontSize="18" Margin="10,5,0,5" Style="{StaticResource Univers57_TextBlock_Style}" Text="Revision" /> <TextBox Grid.Row="4" Grid.Column="1" MaxLength="32" Margin="10,5,0,5" KeyboardNavigation.TabIndex="10" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Text="{Binding CurrentJob.Revision, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Grid.Row="5" FontSize="18" Style="{StaticResource Univers57_TextBlock_Style}" Text="Revision Date" HorizontalAlignment="Right" Margin="10,5,0,5" /> <DatePicker Grid.Row="5" Grid.Column="1" Margin="10,5,0,5" Focusable="True" KeyboardNavigation.TabIndex="11" KeyboardNavigation.TabNavigation="Continue" Style="{StaticResource Base_DatePicker_Template}" Text="{Binding CurrentJob.RevisionDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" /> </Grid>
下层DataPicker的DT是
<Style x:Key="Base_DatePicker_Template" TargetType="{x:Type DatePicker}"> <Setter Property="Foreground" Value="Gray"/> <Setter Property="IsTodayHighlighted" Value="True"/> <Setter Property="SelectedDateFormat" Value="Short"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="Padding" Value="2"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DatePicker}"> <Grid> <Grid x:Name="PART_Root" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <DatePickerTextBox x:Name="PART_TextBox" Height="25" Width="175" Grid.Column="0" Focusable="{TemplateBinding Focusable}" HorizontalContentAlignment="Stretch" Grid.Row="0" Style="{StaticResource Gray_TextBox_Style}" Template="{StaticResource Base_TextBox_Template}" Foreground="#666666" VerticalContentAlignment="Center" Text="{TemplateBinding Text}" Padding="3,5,33,5"/> <Button x:Name="PART_Button" Width="18" Height="18" Cursor="Hand" Grid.Column="1" Foreground="{TemplateBinding Foreground}" Focusable="True" HorizontalAlignment="Right" Margin="5,0" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedDate, Converter={StaticResource DateToDayStringConverter}}" Template="{StaticResource CalenderButton_ControlTemplate}" /> <Grid x:Name="PART_DisabledVisual" IsHitTestVisible="False" Opacity="0"> <Popup x:Name="PART_Popup" AllowsTransparency="True" Placement="Bottom" PlacementTarget="{Binding ElementName=PART_TextBox}" StaysOpen="False"/> </Grid> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
更多参考:
KeyBoardNavigationMode
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/af39c593-6a9f-4899-88b6-b28016de7d6f
Focus overview:
http://msdn.microsoft.com/en-us/library/aa969768.aspx#Keyboard_Navigation
Unable to focus the Calendar button on DatePicker of wpf toolkit
http://msdn.microsoft.com/en-us/library/system.windows.input.keyboardnavigationmode.aspx