I have my own TextBox2 class that derives from TextBox. I want to add a state called TextBlock and I want the VisualStateManager to go to that state when the IsTextBlock property/dependency property is true. When this is true, I want to change the style of the text box to be readonly and look just like a TextBlock but be able to select the text to be copyable. Is this possible? Is there a better way?
+1
A:
Something like that:
[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
Alexander K.
2009-05-26 23:37:47