Solved

Progamatically Adding XAML Controls Problem

Posted on 2014-01-29
6
273 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

943 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now