我已經創建了自定義模板化控件,並將其用作計算器應用程序中的「顯示」。它直接從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>
處理重點輕拍時出現了什麼問題?這很可能是如何在'TextBox'實現中完成的。另外,如果你可以從'TextBox'而不是'Control'繼承,你可能根本不需要實現你自己的焦點邏輯。 –
我曾經從'TextBox'繼承,但我無法完全禁用[選擇](https://i-msdn.sec.s-msft.com/en-us/windows/uwp/input-and -devices/images/select-word.png),每當我點擊TextBox時閃爍的手機上,所以我決定創建自己的控件。 –
隨着Tapped事件的實現,你認爲在Tapped事件中,我應該調用'this.Focus(FocusState.Pointer);'?我認爲它可以通過設置一些屬性來啓用該行爲來自動獲得焦點... –