2017-01-05 9 views
1

私はカスタムテンプレートコントロールを作成しました。私はそれを私の電卓アプリケーションの「ディスプレイ」として使用しています。それはControlクラスから直接継承しますが、私はそれをユーザーのクリックでフォーカス可能にする方法を見つけることができません。私はOnGotFocusメソッドをオーバーライドしていますが、それは決して解雇されません。UWPテンプレートコントロールのフォーカスが得られません

クリックの振る舞いに焦点を当てるためにはいくつかのプロパティが必要だと思いますが、見つけられませんでした。

私はTappedイベントまたはOnTappedメソッドを使用することができますが、それは私が必要とするものではありません。ここで

はGeneric.xamlで私のコードです:

<Style TargetType="controls:ResponsiveTextBox"> 
    <Setter Property="Background" Value="{StaticResource PageBackgroundBrush}"/> 
    <Setter Property="BorderBrush" Value="Transparent"/> 
    <Setter Property="BorderThickness" Value="0,0,0,2"/> 
    <Setter Property="HorizontalContentAlignment" Value="Right"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="HorizontalAlignment" Value="Stretch"/> 
    <Setter Property="VerticalAlignment" Value="Stretch"/> 
    <Setter Property="Padding" Value="10"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="controls:ResponsiveTextBox"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="PointerOver"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement" Storyboard.TargetProperty="BorderBrush"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseLowBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Active"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement" Storyboard.TargetProperty="BorderBrush"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAccentBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement2" Storyboard.TargetProperty="BorderBrush"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 

        <Border x:Name="BackgroundElement" 
          Background="{TemplateBinding Background}" 
          Margin="{TemplateBinding BorderThickness}" 
          Opacity="{ThemeResource TextControlBackgroundRestOpacity}" 
          /> 

        <Border x:Name="BorderElement" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          /> 

        <Border x:Name="BorderElement2" 
          BorderBrush="Transparent" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          /> 

        <TextBlock x:Name="TextElement" 
           Text="{TemplateBinding Text}" 
           Foreground="{TemplateBinding Foreground}" 
           FontSize="{TemplateBinding FontSize}" 
           Margin="{TemplateBinding Padding}" 
           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
           VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
           AutomationProperties.AccessibilityView="Raw" 
           /> 

       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+2

タップでフォーカスを処理すると何が問題になりますか?これは 'TextBox'の実装の中でどのように行われるのでしょうか。また、 'Control'ではなく' TextBox'を継承することができれば、独自のフォーカスロジックを実装する必要はありません。 –

+0

'TextBox'から継承していましたが、[選択]を完全に無効にできませんでした(https://i-msdn.sec.s-msft.com/en-us/windows/uwp/input-and)。私は自分のコントロールを作成することにしたので、TextBoxをタップするたびにそこに点滅していた電話機の-devices/images/select-word.png) –

+0

そして、Tappedイベントの実装では、Tappedイベントでは 'this.Focus(FocusState.Pointer); 'を呼び出すべきだと思いますか?私はそれがその行動を可能にするいくつかのプロパティを設定することによって自動的にフォーカスを得ることができると考えていた... –

答えて

3

コメントセクションで説明したように、TextBox制御と同様の挙動を持つように、手動で内部

this.Focus(FocusState.Programmatic); 

を呼び出す必要がありますそのTappedイベントハンドラ。

+0

私は賞金で答えを与えることができるまで私は5時間待つ必要があります.. –

+0

心配、歓声! –

関連する問題