Solved

Progamatically Adding XAML Controls Problem

Posted on 2014-01-29
6
270 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Total in textboxes 9 35
Crystal Report for VS2013 17 40
Exception in Log4Net 1 19
Why, and when, to use Windows Workflow 1 18
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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
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.

705 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

19 Experts available now in Live!

Get 1:1 Help Now