Thursday, September 4, 2008

Don't Fence Me In (I’m just a TextBox)


In Silverlight 2 Beta 2, setting BorderThickness="0" or BorderBrush="Transparent" on a TextBox fails to hide the border around the TextBox. I couldn't get a TextBox I was building to lose its border:


This seemed like a bug to me, but a Silverlight Program Manager explained it this way in a Silverlight.net thread:
"The new visual style for TextBox uses gradients inside a Grid. This default style ignores the border thickness and border brush."

Since this behavior seems to be intentional, a template needs to be used to hide the border. A poster in the thread above suggests using a ScrollViewer in lieu of the TextBox, and it seemed to work for us:


See below for a quick example of how to implement this.
Abridged XAML for TextBox:
<TextBox Style="{StaticResource TextBoxNoBorder}" x:Name="TextSiteURL" />
Template for the TextBoxNoBorder style:
<Style x:Key="TextBoxNoBorder" TargetType="TextBox">
<
Setter Property="Template">
<
Setter.Value>
<
ControlTemplate TargetType="TextBox">
<
Grid x:Name="RootElement">
<
ScrollViewer x:Name="ContentElement" BorderThickness="0" />
</
Grid>
</
ControlTemplate>
</
Setter.Value>
</
Setter>
</
Style>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.