2009-05-24 10 views
2

私はTextBoxから派生した独自のTextBox2クラスを持っています。私はTextBlockという状態を追加したいので、VisualStateManagerをIsTextBlockプロパティ/依存プロパティがtrueのときにその状態にします。これが当てはまる場合、テキストボックスのスタイルを読み取り専用に変更し、TextBlockのように見せても、コピー可能なテキストを選択できるようにしたいと考えています。これは可能ですか?より良い方法がありますか?Silverlightのコントロールに状態を追加するにはどうすればよいですか?

答えて

2

そのような何か:

[TemplateVisualState(Name = "TextBlock", GroupName = "ControlType")] 
[TemplateVisualState(Name = "TextBox", GroupName = "ControlType")] 
public class TextBox2 : TextBox 
{ 
    public TextBox2() 
    { 
     DefaultStyleKey = typeof (TextBox2); 
     Loaded += (s, e) => UpdateVisualState(false); 
    } 


    private bool isTextBlock; 
    public bool IsTextBlock 
    { 
     get { return isTextBlock; } 
     set 
     { 
      isTextBlock = value; 
      UpdateVisualState(true); 
     } 
    } 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     UpdateVisualState(false); 
    } 


    internal void UpdateVisualState(bool useTransitions) 
    { 
     if (IsTextBlock) 
     { 
      VisualStateManager.GoToState(this, "TextBlock" , useTransitions); 
     } 
     else 
     { 
      VisualStateManager.GoToState(this, "TextBox" , useTransitions); 
     } 
    } 
} 

HTH

関連する問題