【问题标题】:wp7:Custom Control with functionswp7:具有功能的自定义控件
【发布时间】:2011-03-19 20:53:57
【问题描述】:

我正在尝试为我的 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>

【问题讨论】:

    标签: windows-phone-7


    【解决方案1】:

    我想通了。基本上我必须在后面的代码中添加以下内容:

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            GotFocus +=new RoutedEventHandler(OnTextboxInputWordGotFocus);
    
            this.InputScope = new System.Windows.Input.InputScope()
            {
                Names = { new InputScopeName() { NameValue = InputScopeNameValue.Number } }
            };
    
        }
    

    它现在按我想要的方式工作。但是,如果有“更好的方法”可以做到这一点,我愿意接受建议。

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多