2012-03-30 17 views
0

WPFアプリケーションに検索画面があります。この画面は、TabControlのTabItemにUserControlとして実装されています。ユーザーが[検索]タブに切り替えると、特定のフィールドにフォーカスが移ります。私は、フォーカスがどこに向かうかを理解する方法について質問hereを尋ねました。私は今どこに行くのか知っています。今私はそれがそこに行く理由を理解したいので、私はそれを止めることができます。ここで焦点がどこにあるのか知っているので、どうやってそこに行くのか分かりますか?

フォーカスは自発的に変化しており、ユーザーの操作とは何の関係もないことに注意してください。すべてのユーザーが行ったのは、メインウィンドウの検索タブをクリックすることです。フォーカスは、この1つの特定のテキストボックスに移動することになっています。これは、UserControlのLoadedイベントハンドラで行われます。そして、TextBoxに最初に行きます。次に何らかの理由で、CheckBoxに移動します。

ユーザーが操作できるフォームのTabIndexコントロールに値を割り当てました。 CheckBoxはTabIndex 1にあります。問題のTextBoxはTabIndex 9にあります。これはフォーム上の唯一のTextBoxです。

以前は、フォーカスがTextBoxに移動してそこにとどまりました。それを実現することなく、私はCheckBoxにフォーカスを移すようなものを変更しました。 Telerikのコントロールライブラリを最新のバージョンにアップグレードしていた頃になっていたことを、私が変更したことが分かりませんでした。ここで

は全体の制御のためのXAML、マイナスだろうではない問題でいくつかのものです:あなたの助け

ため

<UserControl x:Class="CarSystem.CustomControls.Searcher" 
     <!-- XML Namespaces removed for brevity --> 
     Height="620" 
     Loaded="UserControl_Loaded" 
     Width="990"> 

<UserControl.Resources> 
    <!--- Resource removed for brevity --> 
</UserControl.Resources> 

<Grid Background="{DynamicResource ContentBackground}" 
     FocusManager.IsFocusScope="True" 
     Name="LayoutRoot"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="110" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 

    <Grid Grid.Column="0" 
      Grid.Row="0"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 

     <GroupBox BorderBrush="{DynamicResource ControlBorder}" 
        FontSize="16" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Column="0" 
        Grid.Row="0" 
        Header="Alarm Class:" 
        Margin="5,0"> 
      <Grid VerticalAlignment="Center"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="Auto" /> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="35" /> 
        <RowDefinition Height="*" /> 
       </Grid.RowDefinitions> 
       <CheckBox Click="AllAlarmClasses_Click" 
          Content="ALL" 
          FontSize="14" 
          FontWeight="Bold" 
          Grid.Column="0" 
          Grid.Row="0" 
          HorizontalAlignment="Left" 
          Margin="5" 
          Name="AllAlarmClasses" 
          TabIndex="1" 
          VerticalAlignment="Center" /> 
       <Button Background="{DynamicResource ButtonBackground}" 
         Click="ExpandPicker_Click" 
         Content="Expand" 
         FontSize="14" 
         FontWeight="Bold" 
         Grid.Column="1" 
         Grid.Row="0" 
         Foreground="{DynamicResource ButtonForeground}" 
         Margin="5" 
         Name="ExpandAlarmClass" 
         TabIndex="2" /> 
       <ListBox BorderBrush="Black" 
         BorderThickness="1" 
         CheckBox.Click="AlarmClassPicker_Click" 
         ItemTemplate="{StaticResource CheckableChoice}" 
         FontSize="14" 
         FontWeight="Bold" 
         Grid.Column="0" 
         Grid.ColumnSpan="2" 
         Grid.Row="1" 
         Height="100" 
         ItemsSource="{Binding Path=AlarmClassChoices, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=TwoWay}" 
         Margin="5,0" 
         Name="AlarmClassPicker" 
         SelectionMode="Multiple" 
         TabIndex="3" 
         Visibility="Collapsed" /> 
      </Grid> 
     </GroupBox> 

     <GroupBox BorderBrush="{DynamicResource ControlBorder}" 
        FontSize="16" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Column="0" 
        Grid.Row="1" 
        Header="Source:" 
        Margin="5,0"> 
      <cs:TouchComboBox Background="{DynamicResource UnfocusedBackground}" 
           BorderBrush="{DynamicResource ControlBorder}" 
           FontSize="14" 
           FontWeight="Bold" 
           Foreground="{DynamicResource UnfocusedForeground}" 
           GridBackground="{DynamicResource ContentBackground}" 
           Height="50" 
           IsTabStop="True" 
           ItemsSource="{Binding Path=HotListChoices, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=TwoWay}" 
           Margin="5,0" 
           x:Name="HotListPicker" 
           SelectionChanged="SourcePicker_SelectionChanged" 
           TabIndex="4" 
           TimeOfDayMode="{Binding Path=TimeOfDayMode, RelativeSource={RelativeSource AncestorType={x:Type cs:Searcher}}}" 
           VerticalAlignment="Top" /> 
     </GroupBox> 
     <GroupBox BorderBrush="{DynamicResource ControlBorder}" 
        FontSize="16" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Column="1" 
        Grid.Row="0" 
        Header="Start Date:" 
        Margin="5,0"> 
      <telerik:RadDateTimePicker FontWeight="Bold" 
             Height="35" 
             Margin="5" 
             Name="StartDatePicker" 
             SelectionChanged="DateTimePicker_SelectionChanged" 
             Style="{DynamicResource RadDateTimePickerControlTemplate1}" 
             TabIndex="5" 
             VerticalAlignment="Center" /> 
     </GroupBox> 
     <GroupBox BorderBrush="{DynamicResource ControlBorder}" 
        FontSize="16" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Column="1" 
        Grid.Row="1" 
        Header="End Date:" 
        Margin="5,0"> 
      <telerik:RadDateTimePicker FontSize="14" 
             FontWeight="Bold" 
             Height="35" 
             Margin="5" 
             Name="EndDatePicker" 
             SelectionChanged="DateTimePicker_SelectionChanged" 
             Style="{DynamicResource RadDateTimePickerControlTemplate1}" 
             TabIndex="6" 
             VerticalAlignment="Center" /> 
     </GroupBox> 

     <GroupBox BorderBrush="{DynamicResource ControlBorder}" 
        FontSize="16" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Column="2" 
        Grid.Row="0" 
        Header="State:" 
        Margin="5,0"> 
      <cs:TouchComboBox Background="{DynamicResource UnfocusedBackground}" 
           BorderBrush="{DynamicResource ControlBorder}" 
           DisplayMemberPath="Value" 
           FontSize="14" 
           FontWeight="Bold" 
           Foreground="{DynamicResource UnfocusedForeground}" 
           GridBackground="{DynamicResource ContentBackground}" 
           Height="50" 
           ItemsSource="{Binding Path=LocaleChoices, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
           Margin="5" 
           x:Name="StatePicker" 
           SelectedValue="{Binding Path=LocaleCode}" 
           SelectedValuePath="Key" 
           SelectionChanged="StatePicker_SelectionChanged" 
           TabIndex="7" 
           TimeOfDayMode="{Binding Path=TimeOfDayMode, RelativeSource={RelativeSource AncestorType={x:Type cs:Searcher}}}" 
           VerticalAlignment="Center" /> 
     </GroupBox> 
     <GroupBox BorderBrush="{DynamicResource ControlBorder}" 
        FontSize="16" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Column="2" 
        Grid.Row="1" 
        Header="Plate:" 
        Margin="5,0"> 
      <Border BorderBrush="{DynamicResource ControlBorder}" 
        BorderThickness="1" 
        Height="35" 
        Margin="5" 
        VerticalAlignment="Center"> 
       <TextBox FontSize="14" 
         FontWeight="Bold" 
         GotFocus="PlateBox_GotFocus" 
         LostFocus="PlateBox_LostFocus" 
         Margin="-1" 
         MaxLength="25" 
         MaxLines="1" 
         Name="PlateBox" 
         TabIndex="8" 
         Text="{Binding Path=Plate, Mode=TwoWay}" 
         TextChanged="PlateBox_TextChanged" 
         ToolTip='Wildcards ("%", "_", "[", "]") can be used' /> 
      </Border> 
     </GroupBox> 
    </Grid> 

    <TabControl Background="{DynamicResource TabBackground}" 
       Grid.Row="1" 
       Margin="0,20,0,5" 
       Name="ResultTabs" 
       TabIndex="9"> 

     <TabItem Background="{DynamicResource TabHeaderBackground}" 
       FontSize="16" 
       FontWeight="Bold" 
       Foreground="{DynamicResource TabHeaderForeground}" 
       Header="Hot List Entries:" 
       Name="HotListEntryTab"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="*" /> 
        <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 

       <telerik:RadGridView AutoExpandGroups="True" 
            AutoGenerateColumns="False" 
            CanUserDeleteRows="False" 
            CanUserFreezeColumns="False" 
            CanUserInsertRows="False" 
            CanUserResizeColumns="True" 
            CanUserSortColumns="True" 
            EnableColumnVirtualization="True" 
            EnableRowVirtualization="True" 
            FontSize="14" 
            FontWeight="Bold" 
            IsReadOnly="True" 
            Name="HotListEntriesGrid" 
            SelectionChanged="HotListEntriesGrid_SelectionChanged" 
            SelectionUnit="FullRow" 
            ScrollViewer.CanContentScroll="True" 
            ScrollViewer.HorizontalScrollBarVisibility="Auto" 
            ScrollViewer.VerticalScrollBarVisibility="Auto" 
            TabIndex="10" 
            ToolTip="Matching Hot List Entries"> 
        <telerik:RadGridView.Columns> 
         <telerik:GridViewDataColumn DataMemberBinding="{Binding Plate,  Mode=OneWay}" 
                Header="Plate" 
                Width="*" /> 
         <telerik:GridViewDataColumn DataMemberBinding="{Binding LocaleCode, Mode=OneWay}" 
                Header="State" 
                Width="75" /> 
         <telerik:GridViewDataColumn DataMemberBinding="{Binding ListName, Mode=OneWay}" 
                Header="Source" 
                Width="150" /> 
         <telerik:GridViewDataColumn DataMemberBinding="{Binding AlarmClass, Mode=OneWay}" 
                Header="Alarm Class" 
                Width="150" /> 
         <telerik:GridViewDataColumn DataMemberBinding="{Binding Notes,  Mode=OneWay}" 
                Header="Notes" 
                Width="*" /> 
        </telerik:RadGridView.Columns> 
       </telerik:RadGridView> 
       <cs:ProgressControl FontSize="14" 
            FontWeight="Bold" 
            Grid.Column="0" 
            Grid.Row="1" 
            Height="55" 
            Margin="0,5" 
            x:Name="HotListProgressCtrl" 
            TabIndex="11" 
            TimeOfDayMode="{Binding Path=TimeOfDayMode, RelativeSource={RelativeSource AncestorType={x:Type cs:Searcher}}}" 
            Visibility="Collapsed" /> 
      </Grid> 
     </TabItem> 

     <TabItem Background="{DynamicResource TabHeaderBackground}" 
       FontSize="16" 
       FontWeight="Bold" 
       Foreground="{DynamicResource TabHeaderForeground}" 
       Header="Reads &amp; Alarms:" 
       IsSelected="True" 
       Name="ReadsTabItem"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="*" /> 
        <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 

       <telerik:RadGridView AutoExpandGroups="True" 
            AutoGenerateColumns="False" 
            CanUserDeleteRows="False" 
            CanUserFreezeColumns="False" 
            CanUserInsertRows="False" 
            CanUserResizeColumns="True" 
            CanUserSortColumns="True" 
            EnableColumnVirtualization="True" 
            EnableRowVirtualization="True" 
            FontSize="14" 
            FontWeight="Bold" 
            IsReadOnly="True" 
            Name="ReadsGrid" 
            RowDetailsTemplate="{StaticResource ReadRowDetailsTemplate}" 
            RowStyleSelector="{StaticResource StyleSelector}" 
            SelectionChanged="ReadsGrid_SelectionChanged" 
            SelectionUnit="FullRow" 
            ScrollViewer.CanContentScroll="True" 
            ScrollViewer.HorizontalScrollBarVisibility="Auto" 
            ScrollViewer.VerticalScrollBarVisibility="Auto" 
            ShowGroupFooters="True" 
            TabIndex="12" 
            ToolTip="Matching Reads"> 
        <telerik:RadGridView.Columns> 
         <cs:CustomGridViewToggleRowDetailsColumn IsEnabled="False" 
                   IsFilterable="False" 
                   IsGroupable="False" 
                   ToggleButtonVisibility="{Binding Path=HasAlarms, Converter={StaticResource BoolToVisibility}}" /> 
         <telerik:GridViewDataColumn DataMemberBinding="{Binding Plate,  Mode=OneWay}" 
                Header="Plate" 
                Width="*" /> 
         <telerik:GridViewDataColumn DataMemberBinding="{Binding State,  Mode=OneWay}" 
                Header="State" 
                Width="75" /> 
         <telerik:GridViewDataColumn DataMemberBinding="{Binding TimeStamp, Mode=OneWay, Converter={StaticResource DateConverter}}" 
                Header="Date &amp; Time" 
                Width="150" /> 
         <telerik:GridViewDataColumn DataMemberBinding="{Binding Latitude, Converter={StaticResource CoordConverter}, ConverterParameter=NS}" 
                Header="Latitude" 
                Width="150" /> 
         <telerik:GridViewDataColumn DataMemberBinding="{Binding Longitude, Converter={StaticResource CoordConverter}, ConverterParameter=EW}" 
                Header="Longitude" 
                Width="150" /> 
        </telerik:RadGridView.Columns> 
       </telerik:RadGridView> 
       <cs:ProgressControl FontSize="14" 
            FontWeight="Bold" 
            Grid.Row="1" 
            Height="55" 
            Margin="0,5" 
            x:Name="ProgressCtrl" 
            TabIndex="13" 
            TimeOfDayMode="{Binding Path=TimeOfDayMode, RelativeSource={RelativeSource AncestorType={x:Type cs:Searcher}}}" 
            Visibility="Collapsed" /> 
      </Grid> 
     </TabItem> 
    </TabControl> 

    <GridSplitter Background="{DynamicResource SeparatorColor}" 
        Grid.Row="1" 
        Height="10" 
        HorizontalAlignment="Stretch" 
        Margin="0,5" 
        VerticalAlignment="Top" /> 

    <Grid Grid.Column="1" 
      Grid.RowSpan="2"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="55" /> 
      <RowDefinition Height="55" /> 
      <RowDefinition Height="55" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="55" /> 
      <RowDefinition Height="55" /> 
     </Grid.RowDefinitions> 
     <Button Background="{DynamicResource ButtonBackground}" 
       Click="SearchButton_Click" 
       Content="Search" 
       FontSize="16" 
       FontWeight="Bold" 
       Foreground="{DynamicResource ButtonForeground}" 
       Grid.Row="0" 
       IsDefault="True" 
       Margin="5" 
       Name="SearchButton" 
       TabIndex="14" /> 
     <Button Background="{DynamicResource ButtonBackground}" 
       Click="ClearButton_Click" 
       Content="Clear" 
       FontSize="16" 
       FontWeight="Bold" 
       Foreground="{DynamicResource ButtonForeground}" 
       Grid.Row="1" 
       Margin="5" 
       Name="ClearButton" 
       TabIndex="15" /> 
     <Button Background="{DynamicResource ButtonBackground}" 
       Click="SaveCriteriaButton_Click" 
       FontSize="16" 
       FontWeight="Bold" 
       Grid.Row="2" 
       Foreground="{DynamicResource ButtonForeground}" 
       Margin="5" 
       Name="SaveCriteriaButton" 
       TabIndex="16" 
       Visibility="Visible"> 
      <Button.Content> 
       <TextBlock Text="Save Report" 
          TextAlignment="Center" 
          TextWrapping="Wrap" /> 
      </Button.Content> 
     </Button> 
     <TextBlock FontSize="14" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Row="4" 
        Margin="5" 
        Text="Number of Matches:" 
        TextAlignment="Center" 
        TextWrapping="Wrap" /> 
     <TextBlock FontSize="14" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Row="5" 
        Margin="5" 
        Text="Hot List Entries:" 
        TextAlignment="Center" 
        TextWrapping="Wrap" /> 
     <TextBlock FontSize="14" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Row="6" 
        Margin="5,0,5,10" 
        Text="{Binding Converter={StaticResource LongConverter}, ConverterParameter='#,##0', Path=NoHotListEntries, RelativeSource={RelativeSource AncestorType={x:Type cs:Searcher}}}" 
        TextAlignment="Center" 
        TextWrapping="Wrap" /> 
     <TextBlock FontSize="14" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Row="7" 
        Margin="5" 
        Text="Reads:" 
        TextAlignment="Center" 
        TextWrapping="Wrap" /> 
     <TextBlock FontSize="14" 
        FontWeight="Bold" 
        Foreground="{DynamicResource TextForeground}" 
        Grid.Row="8" 
        Margin="5,0,5,10" 
        Text="{Binding Converter={StaticResource LongConverter}, ConverterParameter='#,##0', Path=NoReads, RelativeSource={RelativeSource AncestorType={x:Type cs:Searcher}}}" 
        TextAlignment="Center" 
        TextWrapping="Wrap" /> 
     <Button Background="{DynamicResource ButtonBackground}" 
       Click="ExportButton_Click" 
       Content="Export" 
       FontSize="16" 
       FontWeight="Bold" 
       Foreground="{DynamicResource ButtonForeground}" 
       Grid.Row="10" 
       Margin="5" 
       Name="ExportButton" 
       TabIndex="17" /> 
     <Button Background="{DynamicResource ButtonBackground}" 
       Click="CloseButtonClicked" 
       Content="Close" 
       FontSize="20" 
       FontWeight="Bold" 
       Foreground="{DynamicResource ButtonForeground}" 
       Grid.Row="11" 
       HorizontalAlignment="Right" 
       Margin="5" 
       Name="CloseButton" 
       TabIndex="18" 
       Width="100" /> 
    </Grid> 

    <Canvas Grid.Column="0" 
      Grid.ColumnSpan="2" 
      Grid.Row="0" 
      Grid.RowSpan="4" 
      Name="KeyboardPopupCanvas"> 
     <cs:KeyboardPopup x:Name="KeyboardPopup" 
          TimeOfDayMode="{Binding Path=TimeOfDayMode, RelativeSource={RelativeSource AncestorType={x:Type cs:Searcher}}}" 
          Title="Keyboard" 
          Visibility="Collapsed" /> 
    </Canvas> 

</Grid> 

おかげでトニー

+0

'tabindex'を設定して、テキストボックスが最初になるようにして、次にタブが押されたときに次にアクセスする各項目を順番に並べてみましょう。これにより、(ランダムな順序ではなく)タブを押すときに論理的な順序でフィールドを記入することができます。これにより、ロードされたイベントのフォーカスを指定する必要がなくなります(最初のtabindexに移動する必要があります)。 – jzworkman

+0

これはおそらく起きていることです。これはおそらく、最も低い番号のtabindexにフォーカスを送ります。私がTextBoxに焦点を当てて、それをやっています。おそらくTextBoxをTabindex 1にするのがいいでしょう。 –

+0

すべてのタブ索引をユーザーが入力する順序(基本的に左から右、上から下)に変更することを忘れないでください。誰かがフォームとやりとりしたとき(キーボードのみの入力を使用) – jzworkman

答えて

2

ちょうどそのように人々この質問を後で見る人は誰でも知っているだろう。私が行った解決策はフォーム上のフィールドを並べ替えることで、最初に焦点を当てたいフィールドが最初にフォーム上のフィールド。 WPFは自動的に、最小のTabIndexでコントロールにフォーカスを与えます。だから、私のコントロールがLoadedイベントハンドラに置かれた後、そのコントロールがフォーカスを失っていたのです。

XamlにTabIndexフィールドセッターがないと、これは決して起こらなかったと思います。生活し、学びます。

+0

数ヶ月後、私のコードを変更していない間に、私が問題を抱えていたのは、私がフォーカスを移していた場所であったかもしれません。私は 'Loaded'イベントハンドラでそれをやっていました。 'ContentRendered'イベントのような他のイベントに入れてしまえば、実際にはうまくいきました。これは将来的に実験する価値があります。 –

関連する問題