none
How to change the content of controltemplate?

    Question

  • Hi everyone, I have a pushpin created by using controltemplate. The pushpin has an image with a SurfaceTextbox. Now I make a copy of that pushpin by using "transferPushpin.Template = pushpin.Template". Everything is copied except the value entered into SurfaceTextbox by user. So I use FindName method to access the SurfaceTextbox of the transferPushpin, but I couldn't because the return result is always null. I thought it should return the SurfaceTextbox because I already created by using "transferPushpin.Template = pushpin.Template". Could you help to solve this problem? Thanks a lot.

    Here is my code:

    //retrieve the value input by user SurfaceTextBox textbox = (SurfaceTextBox)pushpin.Template.FindName("pushpinNumber", pushpin); //copy template transferPushpin.Template = pushpin.Template;

    //access the SurfaceTextBox in transferPushpin SurfaceTextBox textboxTransfer = (SurfaceTextBox)transferPushpin.Template.FindName("pushpinNumber", transferPushpin);

    //assign value to new SurfaceTextBox textboxTransfer.Text = string.Format("{0}", textbox.Text);

    Here is the controltemplate:

    <ControlTemplate x:Key="TemplatePushpin1" TargetType="m:Pushpin">
                <Grid Height="76" Width="50" Background="#53006BFF">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="50"/>
                        <RowDefinition Height="5"/>
                    </Grid.RowDefinitions>
    
                    <Image Source="/BingMapsSurfaceApplication;component/Images/pushpin1.jpeg" Height="50" Width="50" 
                       Grid.Column="0" Grid.Row="0"></Image>
                    <s:SurfaceTextBox Name="pushpinNumber" KeyboardLayout="Numeric" Width="50" Margin="0,0,0,-21" Grid.Row="1" Padding="1" MinHeight="5" TextAlignment="Center"></s:SurfaceTextBox>
                    
                </Grid>
            </ControlTemplate>

    Friday, November 08, 2013 8:51 PM

Answers

  • I'm not too surprised by this. I don't believe Name's work inside of templates because you will likely end up with a lot of objects that use the template and have the same name. Perhaps we should be looking at this from a different perspective. What if you bind the Text property of the Textbox to the data context of the pushpin. You would then be able to get the value of the textbox from the data context of the pushpin.

    http://rbrundritt.wordpress.com

    Monday, November 11, 2013 5:38 PM
    Owner
  • I think you are making this way too complicated. Stat off by creating a simple User control and add that to the map instead of a Pushpin. Since a User Control is a UIElement you should be able to add it to the map in the same way you add a pushpin and have a lot more flexibility.

    http://rbrundritt.wordpress.com

    Monday, November 18, 2013 5:14 PM
    Owner

All replies

  • Try using x:Name instead of Name.

    http://rbrundritt.wordpress.com

    Monday, November 11, 2013 9:59 AM
    Owner
  • I changed that but it didn't work. The return value of this line is always null.

    SurfaceTextBox textboxTransfer = (SurfaceTextBox)transferPushpin.Template.FindName("pushpinNumber", transferPushpin);

    Do you have any other ideas? Thanks a lot.

    Monday, November 11, 2013 4:15 PM
  • I'm not too surprised by this. I don't believe Name's work inside of templates because you will likely end up with a lot of objects that use the template and have the same name. Perhaps we should be looking at this from a different perspective. What if you bind the Text property of the Textbox to the data context of the pushpin. You would then be able to get the value of the textbox from the data context of the pushpin.

    http://rbrundritt.wordpress.com

    Monday, November 11, 2013 5:38 PM
    Owner
  • So you mean it should be like this:

    <s:SurfaceTextBox Text="{Binding Path=DataContext.Pushpin}"></s:SurfaceTextBox>

    Thanks a lot.

    • Edited by Faro Luce Tuesday, November 12, 2013 9:56 PM
    Tuesday, November 12, 2013 9:43 PM
  • Hi Rick, I tried to bind the Text property of the Textbox to the data context of the pushpin, but it still didn't work. The new textbox doesn't have value of the old textbox. Could you help me to solve this? Thanks.
    Monday, November 18, 2013 5:08 PM
  • I think you are making this way too complicated. Stat off by creating a simple User control and add that to the map instead of a Pushpin. Since a User Control is a UIElement you should be able to add it to the map in the same way you add a pushpin and have a lot more flexibility.

    http://rbrundritt.wordpress.com

    Monday, November 18, 2013 5:14 PM
    Owner
  • So you mean I have to create another XAML file having the image and textbox in it, and then I add that user control to the map? If so, is that user control still has the properties of a draggable pushpin? Thanks.
    Monday, November 18, 2013 5:23 PM