2011-03-19 91 views
2

我想爲我的WP7應用程序構建一個自定義的文本框控件。基本上我希望它有一個的GotFocus功能,我希望能夠使它有一些InputScopewp7:自定義控件的功能

我使用以下資源作爲我的基地,試圖創建一個文本框自定義控制:

我可以得到文本框在我的應用程序中顯示,但我不能在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> 

回答

1

我想通了。基本上我不得不添加以下代碼到後面的代碼:

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

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

    } 

它正在工作,我現在想要的。但是,如果有這樣做的「更好的方式」,我願意提供建議。

謝謝!