wp7:自定义控件的功能
问题描述:
我想为我的WP7应用程序构建一个自定义的文本框控件。基本上我希望它有一个的GotFocus功能,我希望能够使它有一些InputScopewp7:自定义控件的功能
我使用以下资源作为我的基地,试图创建一个文本框自定义控制:
- http://www.windowsphonegeek.com/articles/WP7-WatermarkedTextBox-custom-control
- http://www.windowsphonegeek.com/articles/Creating-a-WP7-Custom-Control-in-7-Steps
我可以得到文本框在我的应用程序中显示,但我不能在GotFocus呼叫,而无需在应用程序中的功能(这违背了目的)工作。
我通常会调用的GotFocus函数也在genericTextbox的类中。我将如何调用GotFocus和InputScope?
的资源字典如下:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:local="clr-namespace:wp7CustomControlsLibrary">
<Style TargetType="local:genericTextbox">
<Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
<Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
<Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
<Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>
<Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/>
<Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/>
<Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:genericTextbox">
<Grid Background="Transparent">
<Border x:Name="EnabledBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}">
<Grid>
<ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答
我想通了。基本上我不得不添加以下代码到后面的代码:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
GotFocus +=new RoutedEventHandler(OnTextboxInputWordGotFocus);
this.InputScope = new System.Windows.Input.InputScope()
{
Names = { new InputScopeName() { NameValue = InputScopeNameValue.Number } }
};
}
它正在工作,我现在想要的。但是,如果有这样做的“更好的方式”,我愿意提供建议。
谢谢!