Solved

Progamatically Adding XAML Controls Problem

Posted on 2014-01-29
6
277 Views
Last Modified: 2014-02-03
I'm trying to add controls programmatically in my WP8 project but get an error that the StackPanel to which I'm trying to add the controls to is not declared.

Is there some limitation on adding controls in a DataTemplate or is something else going wrong?

       
   <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <phone:LongListSelector Name="ListItems">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Name="DataPanel" Orientation="Horizontal">
                            <CheckBox Name="CheckBoxComplete" IsChecked="{Binding sActive, Mode=TwoWay}" Checked="CheckBoxComplete_Checked" Content="{Binding sStartDate}" Margin="10,5" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </Grid>

Open in new window


            Dim newBtn As New System.Windows.Controls.Button
            newBtn.Content = "A New Button"
            DataPanel.Children.Add(newBtn)

Open in new window

0
Comment
Question by:Dodsworth
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:jasonduan
ID: 39818354
"DataPanel" is not available directly in your code because it is inside the "ItemTemplate", which means there maybe multiple items that contains "DataPanel".

If you really need access "DataPanel" in your code, you'll have to iterate the items, and use "FindName()" method to find it.
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39820208
Could I somehow generate the whole datatemplate and add that in code?
0
 
LVL 11

Expert Comment

by:jasonduan
ID: 39821597
I am not sure of that and i would not do that. But i am curious of why you need to "generate the whole datatemplate and add that in code"? What do you really want to achieve? Maybe there is a better solution othan than doing this?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:Dodsworth
ID: 39823598
Well It all started with another post..

Original Question

I thought that if I queried the fields of the datasource using reflection, then I could programmatically add the correct type of control to the datatemplate.
0
 
LVL 11

Accepted Solution

by:
jasonduan earned 500 total points
ID: 39824196
Replied to your original question. Let me know if it works or not.
0
 
LVL 1

Author Closing Comment

by:Dodsworth
ID: 39829778
Double the points :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question