2010-04-12 70 views
0

我有這個自定義文本框,我正在處理,我可以在xaml中使用它,但是當我運行我的應用程序時,我無法選擇它或鍵入它。這裏是我的代碼:WPF-爲什麼我的自定義文本框不能被選中?

public class ModdedTextBox : TextBox 
{ 
    private bool selectionStartChangeFromUI; 
    private bool selectionLengthChangeFromUI; 
    private bool selectedTextChangeFromUI; 

    static ModdedTextBox() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(ModdedTextBox), new FrameworkPropertyMetadata(typeof(ModdedTextBox))); 
     //this.SelectionChanged += this.OnSelectionChanged; 
     //PropertyDescriptor VerticalOffsetProperty = TypeDescriptor.GetProperties(typeof(ModdedTextBox))["VerticalOffset"]; 
     //VerticalOffsetProperty.AddValueChanged(this, this.OnVerticalOffsetChanged); 
    } 
    public static readonly DependencyProperty BindableSelectionStartProperty = 
    DependencyProperty.Register(
    "BindableSelectionStart", 
    typeof(int), 
    typeof(ModdedTextBox), 
    new PropertyMetadata(OnBindableSelectionStartChanged)); 

    public static readonly DependencyProperty BindableSelectionLengthProperty = 
     DependencyProperty.Register(
     "BindableSelectionLength", 
     typeof(int), 
     typeof(ModdedTextBox), 
     new PropertyMetadata(OnBindableSelectionLengthChanged)); 

    public static readonly DependencyProperty BindableSelectedTextProperty = 
     DependencyProperty.Register(
     "BindableSelectedText", 
     typeof(string), 
     typeof(ModdedTextBox), 
     new PropertyMetadata(OnBindableSelectedTextChanged)); 
    public static readonly DependencyProperty DelayedTextProperty = 
     DependencyProperty.Register(
     "DelayedText", 
     typeof(string), 
     typeof(ModdedTextBox), 
     new PropertyMetadata(OnDelayedTextChanged)); 

    public int BindableSelectionStart 
    { 
     get 
     { 
      return (int)this.GetValue(BindableSelectionStartProperty); 
     } 

     set 
     { 
      this.SetValue(BindableSelectionStartProperty, value); 
     } 
    } 

    public int BindableSelectionLength 
    { 
     get 
     { 
      return (int)this.GetValue(BindableSelectionLengthProperty); 
     } 

     set 
     { 
      this.SetValue(BindableSelectionLengthProperty, value); 
     } 
    } 
    public string BindableSelectedText 
    { 
     get 
     { 
      return (string)this.GetValue(BindableSelectedTextProperty); 
     } 

     private set 
     { 
      this.SetValue(BindableSelectedTextProperty, value); 
     } 
    } 
    public string DelayedText 
    { 
     get 
     { 
      return (string)this.GetValue(DelayedTextProperty); 
     } 

     private set 
     { 
      this.SetValue(DelayedTextProperty, value); 
     } 
    } 

    private static void OnBindableSelectionStartChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
    { 
     var textBox = dependencyObject as ModdedTextBox; 

     if (!textBox.selectionStartChangeFromUI) 
     { 
      int newValue = (int)args.NewValue; 
      textBox.SelectionStart = newValue; 
     } 
     else 
     { 
      textBox.selectionStartChangeFromUI = false; 
     } 
    } 

    private static void OnBindableSelectionLengthChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
    { 
     var textBox = dependencyObject as ModdedTextBox; 

     if (!textBox.selectionLengthChangeFromUI) 
     { 
      int newValue = (int)args.NewValue; 
      textBox.SelectionLength = newValue; 
     } 
     else 
     { 
      textBox.selectionLengthChangeFromUI = false; 
     } 
    } 
    private static void OnBindableSelectedTextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
    { 
     var textBox = dependencyObject as ModdedTextBox; 

     if (!textBox.selectedTextChangeFromUI) 
     { 
      string newValue = (string)args.NewValue; 
      textBox.BindableSelectedText = newValue; 
     } 
     else 
     { 
      textBox.selectedTextChangeFromUI = false; 
     } 
    } 
    private static void OnDelayedTextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
    { 
    } 
    private void OnSelectionChanged(object sender, RoutedEventArgs e) 
    { 
     if (this.BindableSelectionStart != this.SelectionStart) 
     { 
      this.selectionStartChangeFromUI = true; 
      this.BindableSelectionStart = this.SelectionStart; 
     } 

     if (this.BindableSelectionLength != this.SelectionLength) 
     { 
      this.selectionLengthChangeFromUI = true; 
      this.BindableSelectionLength = this.SelectionLength; 
     } 
     if (this.BindableSelectedText != this.SelectedText) 
     { 
      this.selectedTextChangeFromUI = true; 
      this.BindableSelectedText = this.SelectedText; 
     } 
    } 
    private void OnVerticalOffsetChanged(object sender, EventArgs e) 
    { 
     MessageBox.Show("hello the vertical offset works"); 
    } 
} 

回答

1

你的控制需要一種風格來顯示自己。出從構造這一行 評論,使用默認的樣式

DefaultStyleKeyProperty.OverrideMetadata(typeof(ModdedTextBox), new FrameworkPropertyMetadata(typeof(ModdedTextBox))); 

完成!

編輯:或者這會明確地讓你控制使用文本框風格

DefaultStyleKeyProperty.OverrideMetadata(
    typeof(ModdedTextBox), 
    new FrameworkPropertyMetadata(typeof(TextBox))); 
+0

謝謝majocha!我知道我必須錯過簡單的東西。 – Justin 2010-04-12 23:25:14

相關問題