TextBox with Strikethrough in Silverlight by modifying the TextBox template

Someone recently asked about a strikethrough in a TextBlock.  The need was for a short piece of text that was dynamically generated.  I messed with some solutions and came up with the following:

image

I used a TextBox rather than the TextBlock.  If you don’t want it to be editable, I suggest isReadOnly to true.  You can change the read only part of the template if you don’t want it to look different in Read only view.

Put a TextBox on the form.  Right click on the TextBox and choose EditTemplate  -> Edit a copy.

Inside of the Template, traverse the tree RootElement –> Border –> Grid –> MouseOverBoarder –> ContentElement (see below). 

image

Right click on ContentElement choose Edit Template –> Edit a Copy.

this brings you to the ScrollViewer Template.  Here I added a straight red line as a path and named it StrikeThroughPath

image

Now the trick is to bind the width element to the Template Binding –> Extent Width by clicking on the Advanced square to the right of width and choose Template Binding then Extent Width.

image

You should now have a text box that looks like the one at the start of this post with variable length and all the attributes of a textbox.

My husband, Robert, found this solution in the Silverlight forums but I did not try it http://forums.silverlight.net/forums/p/19749/67384.aspx

Please post any better solutions or questions that you have.

Comments

# re: TextBox with Strikethrough in Silverlight by modifying the TextBox template
Gravatar so important to be
Left by lilly on 5/18/2012 10:41 PM
Comments have been closed on this topic.