微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight入门学习28

原文地址: http://www.dingos.cn/index.php?topic=2000.0

第二十八章   如何在 Silverlight 页面上弹出层?

xaml 页面添加一个按钮,如下所示:

< Grid x : Name ="LayoutRoot" Background ="White" >

    < Button Width ="100" Height ="50" x : Name ="showPopup" Click ="showPopup_Click"

         Content ="Show Popup" />

</ Grid >

在后置代码文件 page.xaml.cs )中添加如下代 码

Popup p = new Popup ();

private void showPopup_Click(object sender,RoutedEventArgs e) {

    // Create a panel control to host other controls

    StackPanel panel1 = new StackPanel ();

    panel1.Background = new SolidColorBrush (Colors .Gray);

 

    // Create a button

    Button button1 = new Button ();

    button1.Content = "Close" ;

    button1.Margin = new Thickness (5.0);

    button1.Click += new RoutedEventHandler (button1_Click);

 

    // Create a text label

    TextBlock textblock1 = new TextBlock ();

    textblock1.Text = "The popup control" ;

    textblock1.Margin = new Thickness (5.0);

 

    // Add text label and button to the panel

    panel1.Children.Add(textblock1);

    panel1.Children.Add(button1);

 

    // Now,make the panel a child of the popup so that

    // the panel will be shown within the Popup when displayed.

    p.Child = panel1;

 

    // Set the position.

    p.VerticalOffset = 25;

    p.HorizontalOffset = 25;

 

    // Show the popup.

    p.IsOpen = true ;

}

 

void button1_Click(object sender,RoutedEventArgs e) {

    // Close the popup.

    p.IsOpen = false ;

}

注意:需要添加 System.Windows.Controls.Primitives 命名控件的引用。

运行应用程序。可以看见页面上有一个按钮。当点击按钮时,一个具有文本标签和按钮的弹出层会显 示。当点击弹出层中按钮,将会关闭弹出层。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐