?
Solved

Progamatically Adding XAML Controls Problem

Posted on 2014-01-29
6
Medium Priority
?
303 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 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