tags:

views:

39

answers:

1

I would like to retrieve a list of links from SQLServer, and programmatically create some HyperlinkButtons from that list. These buttons should be added to a StackPnael. What is the best way to do this?

Something along the lines of:

    private void RefreshMenu()
    {
        var dc = new FrameworkCMSDomainContext();
        var query = dc.GetCMSPagesForSectionQuery(Section);

        dc.Load(query, (s) =>
        {
            foreach(var page in dc.CMSPages)
            {
                HyperlinkButton btn = new HyperlinkButton();
                btn.NavigateUri = new Uri("/" + Section + "/" + page.Name, UriKind.Relative);
                btn.Content = page.Name;
                btn.TargetName = "ContentFrame";
                //Add to stackpanel here
            }

        }, null);
    }

    <Grid x:Name="LayoutRoot" Background="White">
    <StackPanel x:Name="LinksStackPanel" Orientation="Vertical">

    </StackPanel>
    <Button x:Name="AddPage" Click="AddPage_Click">Add</Button>

</Grid>
+1  A: 
LinksStackPanel.Children.Add(btn);
Stephan
Error 3 The name 'LinkStackPanel' does not exist in the current context
cmaduro
Is the code you posted directly from the code behind of the UserControl that contains the XAML at the bottom? The value in `x:Name` of your XAML is the name you can use to access the control from the code behind. (Check that you spelled it correctly, your comment is missing as s)
Stephan
That is true, i corrected it. But the error still persists. In fact I spelled it wrong because it was not in the intellisense popup list.
cmaduro
Is your `RefreshMenu()` method in the code behind of the UserControl? That is the only place that LinksStackPanel will be recognized.
Stephan
I notice that the generated file is not correct (VerticalMenu.g.i.cs) None of the controls are present.
cmaduro
I just had to create a new user control, and copy over the code. Strange why sometimes in VS2010 the auto-generated files will just quit on me.
cmaduro
The linked, auto-gen files sometimes have some strange issues. Anytime you have weird issues like that you should be able to delete the file itself and it should regenerate it for you.
Stephan