Silverlight 4:将HyperlinkBut​​ton动态添加到stackpanel

时间:2010-10-08 20:02:50

标签: silverlight

我想从SQLServer检索链接列表,并以编程方式从该列表中创建一些HyperlinkBut​​tons。这些按钮应添加到StackPnael。这样做的最佳方式是什么?

有些事情:

    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 个答案:

答案 0 :(得分:3)

LinksStackPanel.Children.Add(btn);