Windows phone 7 wp7:具有函数的自定义控件
我正在尝试为我的WP7应用程序构建一个自定义文本框控件。基本上,我希望它有一个GotFocus函数,我希望它有一个数字InputScope 我正在使用以下资源作为尝试创建textbox自定义控件的基础:Windows phone 7 wp7:具有函数的自定义控件,windows-phone-7,Windows Phone 7,我正在尝试为我的WP7应用程序构建一个自定义文本框控件。基本上,我希望它有一个GotFocus函数,我希望它有一个数字InputScope 我正在使用以下资源作为尝试创建textbox自定义控件的基础: 我可以让文本框显示在我的应用程序中,但如果没有应用程序中的功能,我就无法让GotFocus调用正常工作(这违背了目的) 我通常调用的GotFocus函数也在genericTextbox类中。我如何调用GotFocus和InputScope ResourceDictionary如下所示:
<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 } }
};
}
它现在正按我所希望的方式工作。然而,如果有“更好的方法”这样做,我愿意接受建议
谢谢