2013-03-28 20 views
0

這裏是我的自定義控件改變,與自定義XAML樣式:我有一個基於文本框自定義控制,但文字並不在框TextChanged處理程序

using System.Windows.Controls; 

namespace MyControls 
{ 
    public class CustomTextBox : TextBox 
    { 
    } 
} 

<Style TargetType="controls:CustomTextBox"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="controls:CustomTextBox"> 
       <StackPanel> 
        <TextBlock Text="" /> 
        <TextBox Text="{TemplateBinding Text}" /> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

而且使用時,TextChanged處理程序沒有按」由於某些原因,不能獲得更新的Text字段。

<controls:CustomTextBox x:Name="ControlInstance" 
         TextChanged="OnTextChanged" 
         InputScope="EmailNameOrAddress" /> 

private void OnTextChanged(object sender, TextChangedEventArgs e) 
{ 
    // ControlInstance.Text is always ""! 
} 

回答

1

那是因爲你改變內TextBox的Text財產在你的模板,但不是你的CustomTextBox本身的Text財產。

我建議您查看http://www.geekchamp.com/articles/creating-a-wp7-custom-control-in-7-steps以獲取有關創建自定義控件的幫助。

+0

你能詳細說說嗎?我以爲我是從'TextBox'繼承'Text'屬性。 – moswald

+1

你是。但是,您在模板中放置了*第二個* TextBox,並將Text屬性綁定到內部TextBox。 –

+0

對,這很有道理。謝謝。 – moswald

相關問題