2012-02-16 56 views
1

道歉,如果這個問題的答案是完全明顯的,但它目前我難住。爲什麼這個例子中的文本框沒有獲得焦點?如果我使用沒有控制模板的香草文本框,焦點工作正常。控制模板文本框焦點

<StackPanel> 
     <Label Name="lblChartTitle" 
      Content="{x:Static res:Strings.ChartOptionsTitlesControlView_Label_Title}" /> 
      <TextBox Name="txtChartTitle" 
        Text="{Binding Path=ChartTitle}" 
        MaxLength="255" 
        KeyboardNavigation.TabIndex="1" 
        Template="{DynamicResource ctTextBox3DInset}" 
        /> 
     <Label Name="lblChartCategoryXAxis" 
      Content="{x:Static res:Strings.ChartOptionsTitlesControlView_Label_CategoryXAxis}" /> 
      <TextBox Name="txtChartCategoryXAxis" 
        Text="{Binding Path=CategoryXAxis}" 
        MaxLength="255" 
        KeyboardNavigation.TabIndex="2" 
        Template="{DynamicResource ctTextBox3DInset}" 
        /> 
     <Label Name="lblChartValueYAxis" 
      Content="{x:Static res:Strings.ChartOptionsTitlesControlView_Label_ValueYAxis}" /> 
      <TextBox Name="txtChartValueYAxis" 
        Text="{Binding Path=ValueYAxis}" 
        MaxLength="255" 
        KeyboardNavigation.TabIndex="3" 
        Template="{DynamicResource ctTextBox3DInset}" 
        /> 
    </StackPanel> 

<ControlTemplate x:Key="ctTextBox3DInset" TargetType="TextBox"> 
      <Border 
       Style="{StaticResource BorderStyle3DInsetBlack}" 
       Margin="0,0,0,5"> 
       <Border Style="{StaticResource BorderStyle3DInsetWhite}"> 
        <Border Style="{StaticResource BorderStyle3DInset}"> 
         <TextBox 
          TabIndex="{TemplateBinding TabIndex}" 
          BorderThickness="0"/> 
        </Border> 
       </Border> 
      </Border> 
     </ControlTemplate> 

回答

1

因爲當你申請的控件模板,你實際上是創建另一個文本框。所以焦點不在設置爲您的控件模板中的文本框中。

編輯:

你需要使用什麼實際<ContentPresenter/>是:

<ContentPresenter/> 

,而不是創建使用

<TextBox TabIndex="{TemplateBinding TabIndex}" 
         BorderThickness="0"/> 

編輯2一個TextBox:我覺得我以前的答案是錯誤,TextBox的工作方式不同。 您必須爲文本框使用<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>,PART_ContentHost是用於將元素分配爲內容主機的特殊名稱。並且必須與ScrollViewer或AdornerDecorator一起使用。

這是reference

+0

感謝您的回答。我正在使用.NET 3.5,我無法找到ControlPresenter。你的意思是ContentPresenter? – dior001 2012-02-16 15:05:17

+0

Woops,對我的錯字感到抱歉。是的,我的意思是。 – 2012-02-16 15:06:34