Click here to Skip to main content
15,913,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple question about XAML.

I created a grid element inside another grid and added a textblock and a web browser in it. However, I am unable to access it in MainPage.xaml.cs using their name (e.g. this.LastName) doesn't work. On further debugging, I saw that they are not declared in MainPage.g.cs. Since MainPage.g.cs is auto defined, I don't know how to fix it. Can someone help please? Below is my C# and XAML code. Thanks!
========================================================
public partial class MainPage : Microsoft.Phone.Controls.PhoneApplicationPage {
internal System.Windows.Controls.Grid LayoutRoot;
internal System.Windows.Controls.Grid ContentPanel;
internal Microsoft.Phone.Controls.LongListSelector MainLongListSelector;
internal System.Windows.Controls.Image RefreshIcon;
private bool _contentLoaded;

public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Windows.Application.LoadComponent(this, new System.Uri("/Suod;component/MainPage.xaml", System.UriKind.Relative));
this.LayoutRoot = ((System.Windows.Controls.Grid)(this.FindName("LayoutRoot")));
this.ContentPanel = ((System.Windows.Controls.Grid)(this.FindName("ContentPanel")));
this.MainLongListSelector = ((Microsoft.Phone.Controls.LongListSelector)(this.FindName("MainLongListSelector")));
this.RefreshIcon = ((System.Windows.Controls.Image)(this.FindName("RefreshIcon")));
}
========================================================
XML
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <phone:LongListSelector x:Name="MainLongListSelector" Margin="-12,-97,0,97" ItemsSource="{Binding Items}">
        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="12,100,0,45">
                    <Grid x:Name="CompanyContentGrid">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Name="LastName" Text="{Binding Name}" TextWrapping="Wrap" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
                        <phone:WebBrowser Grid.Row="5" Height="400" Name="WebBrowser" Margin="12,-6,24,0" FontFamily="Portable User Interface"/>
                    </Grid>
                </StackPanel>
            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
    </phone:LongListSelector>
</Grid>
<Grid Grid.Row="2" >
    <Image Source="/Assets/Refresh.png" Name="RefreshIcon"  Width="80" Height="80" Tap="Image_Tap"/>
</Grid>
Posted
Comments
Sergey Alexandrovich Kryukov 19-Jul-14 22:46pm    
Apparently, LastName is defined, as well as WebBrowser. From your information, it's hard to see what are you missing. Your "this" depends on context... :-)
—SA

1 solution

I'm sorry to tell you this, but the Grid's name was declared within a template. The template is what will be used to create a new Grid, and the name within the template is for template reference only. The same thing goes for that TextBlock and WebBrowser items. The only named items you can access are the ContentPanel, the MainLongListSelector, and the RefreshIcon that's assuming that the ContentPanel item and the other Grid are not inside an other template.

Another way to look at it is this. That template is a cookie cutter that will be applied to each item in MainLongListSelector. So, each item would have a Grid named ContentPanel, a TextBlock named LastName, etc., etc..

I'm sorry that I can't give you an actual solution to your problem, I don't know what you are trying to do besides access those items. The fact that you are trying to use the this keyword to reference the items makes me think that you only expect one instance of the named items to exist, but I don't know if that's because you only expect there to be one item in the selector.
 
Share this answer
 
Comments
SweetNeetu 20-Jul-14 0:33am    
Thanks Francisco. Here is my scenario: I have a xml file where there are a list of items (with name & hyperlink). At any time, I expect to show only one item (username & hyperlink) on my page. When user click refresh button, I want to show next item in list.
Francisco T. Chavez 20-Jul-14 1:05am    
In that case I would advise you to create a UserControl that uses the selected item as its DataContext. This control would contain your WebBrowser, and you can have it refresh eachtime the DataContext is changed. Then have the refresh button raise an event that tells the control that contains your list to select the next item in the list.

You might even be able to get rid of the visual list, and just have some kind of list or array in the code behind that feeds you one item at a time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900