2009-05-24 6 views
2

TextBox에서 파생 된 자체 TextBox2 클래스가 있습니다. TextBlock이라는 상태를 추가하고 IsTextBlock 속성/dependency 속성이 true 일 때 VisualStateManager를 해당 상태로 전환합니다. 이것이 사실 일 때 텍스트 상자의 스타일을 읽기 전용으로 변경하고 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

관련 문제