wp7:具有功能的自定义控件

时间:2011-03-19 20:53:58

标签: windows-phone-7

我正在尝试为我的WP7应用构建自定义文本框控件。基本上我希望它有一个GotFocus功能,我希望能够让它有一个数字InputScope

我使用以下资源作为尝试创建文本框自定义控件的基础:

我可以让文本框显示在我的应用程序中,但是如果没有应用程序中的功能(这会破坏目的),我无法让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>

1 个答案:

答案 0 :(得分:1)

我明白了。基本上我不得不在后面的代码中添加以下内容:

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        GotFocus +=new RoutedEventHandler(OnTextboxInputWordGotFocus);

        this.InputScope = new System.Windows.Input.InputScope()
        {
            Names = { new InputScopeName() { NameValue = InputScopeNameValue.Number } }
        };

    }

现在我正在努力工作。但是,如果有“更好的方法”,我愿意接受建议。

谢谢!