C# 如何在Silverlight中向控件添加状态?

C# 如何在Silverlight中向控件添加状态?,c#,.net,silverlight,C#,.net,Silverlight,我有自己的TextBox2类,它是从TextBox派生的。我希望添加一个名为TextBlock的状态,并希望VisualStateManager在IsTextBlock属性/依赖项属性为true时进入该状态。如果这是真的,我想将文本框的样式更改为只读,看起来就像一个文本块,但能够选择要复制的文本。这可能吗?有更好的方法吗?类似的方法: [TemplateVisualState(Name = "TextBlock", GroupName = "ControlType")] [TemplateVis

我有自己的TextBox2类,它是从TextBox派生的。我希望添加一个名为TextBlock的状态,并希望VisualStateManager在IsTextBlock属性/依赖项属性为true时进入该状态。如果这是真的,我想将文本框的样式更改为只读,看起来就像一个文本块,但能够选择要复制的文本。这可能吗?有更好的方法吗?

类似的方法:

[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);
        }
    }
}