tags:

views:

37

answers:

3

I have a bunch of textboxes on a XAML page that I wanted the same size. I created a control template and put it in the Grid.Resources section of the page

<Grid.Resources>
    <ControlTemplate x:Key="basicTextbox" TargetType="TextBox" >
        <TextBox MinWidth="200" />
    </ControlTemplate>
</Grid.Resources>

and I apply it to a textbox like the following:

<TextBox x:Name="txtNewSec1" Template="{StaticResource basicTextbox}"/>

I have a button that a user can press and in the code behind I take the text the user has entered and apply it to an object. I was surprised everytime when the text would come back blank when text was in the textbox. After removing the template from the textbox and clicking the button again, the text is magically available during the button's click event handler. Is there something I have to set in the ControlTemplate to allow the textbox to have text during code-behind events? Or is this some sort of bug in Silverlight?

A: 

It seemed odd you have a TextBox in the template of a TextBox.

Is that not creating a second TextBox inside the first?

When I mock-up up your example and assign a Text value to the Template TextBox it shows immediately, whereas any Text value in the later instance does nothing.

Enough already
I was thinking that myself....I think I need to look at property setters
Josh
+1  A: 

Changed to a style and it seems to work:

<Style x:Key="basicTextbox" TargetType="TextBox" >
    <Setter Property="MinWidth" Value="200" />
</Style>

and on the textbox changed 'Template' to 'Style'

<TextBox x:Name="txtNewSec1" Style="{StaticResource basicTextbox}" />
Josh
@Josh: That was my next suggestion. That will do what you want. +1 for solving it yourself :)
Enough already
+3  A: 

You shouldn't use a control template to achieve what you want to do. What you need is... styling (tada)

<Grid.Resources>
  <Style x:Key="basicTextBox" TargetType="TextBox">
    <Setter Property="MinWidth" Value="200"/>
  </Style>
</Grid.Resources>

and:

<TextBox x:Name="txtNewSec1" Style="{StaticResource basicTextbox}"/>
Maupertuis
@Maupertuis: Correct, just 60 seconds too late. Josh figured that one out :) +1 for getting it right.
Enough already
Yes, I saw it when I sent the answer... too late. ;) But better twice than not. thanks
Maupertuis