使用silverlight创建弹出窗口并向弹出窗口添加数据网格

时间:2014-08-21 15:25:09

标签: c# wpf silverlight popup

我需要在Silverlight项目的弹出窗口中添加一个网格。我在mainpage.xaml.cs文件中添加了这段代码,我创建了一个弹出窗口。 但我需要添加一个显示数据库表格的网格。

    private void Button_Click_1(object sender, RoutedEventArgs e)

    {

        popup p = new Popup();Border border = new Border();
        border.BorderBrush = new SolidColorBrush(Colors.Black);
        border.BorderThickness = new Thickness(5.0);

        StackPanel panel1 = new StackPanel();
        panel1.Background = new SolidColorBrush(Colors.LightGray);

        Button button1 = new Button();
        button1.Content = "Close";
        button1.Margin = new Thickness(2.0);
        button1.Click += new RoutedEventHandler(button1_Click);
        TextBlock textblock1 = new TextBlock();
        textblock1.Text = "The popup control";
        textblock1.Margin = new Thickness(5.0);
        panel1.Children.Add(textblock1);
        panel1.Children.Add(button1);
        border.Child = panel1;

        Grid grid1 = new Grid();
        // i need to bind this grid to data from the sql database and attach this grid to the  popup     


        // Set the Child property of Popup to the border 
        // which contains a stackpanel, textblock and button.
        p.Child = border;
        p.DataContext = 
        // Set where the popup will show up on the screen.
        p.VerticalOffset = 200;
        p.HorizontalOffset = 300;

        // Open the popup.
        p.IsOpen = true;


    }

1 个答案:

答案 0 :(得分:0)

网格是一种布局控件,而不是用于显示表格。对于表格数据,您需要一个DataGrid控件。

List<Customer> customerList = new List<Customer>();

var dataGrid = new DataGrid();
dataGrid.ItemsSource = customerList;

Popup popup = new Popup();
popup.Child = dataGrid;
popup.IsOpen = true;