Solved

Progamatically Adding XAML Controls Problem

Posted on 2014-01-29
6
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 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