开发者

wp7:Custom Control with functions

开发者 https://www.devze.com 2023-02-18 01:55 出处:网络
I am trying to build a custom textbox control for my WP7 app.Basically I want it to have a GotFocus function and I would like to be able to make it have a number InputScope

I am trying to build a custom textbox control for my WP7 app. Basically I want it to have a GotFocus function and I would like to be able to make it have a number InputScope

I am using the following resources as my base for trying to create a textbox custom con开发者_如何学JAVAtrol:

  • http://www.windowsphonegeek.com/articles/WP7-WatermarkedTextBox-custom-control
  • http://www.windowsphonegeek.com/articles/Creating-a-WP7-Custom-Control-in-7-Steps

I can get the textbox to display in my app, but I can not get the GotFocus call to work without having the function in the app (which defeats the purpose).

The GotFocus function that I would normally call is also in the class of the genericTextbox. How would I call the GotFocus and the InputScope?

The ResourceDictionary is the following:

<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>


I figured it out. Basically I had to add the following to the code behind:

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

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

    }

It is working how I want now. However, if there are "better ways" of doing this, I'm open to suggestions.

Thanks!

0

精彩评论

暂无评论...
验证码 换一张
取 消