首先创建一个Silverlight项目,紧接着创建Silverlight子窗体,命名为MyChildWindow.xaml
先看一下MainPage.xaml界面:
子页面MyChildWindow.xaml界面:
MainPage.xaml具体实现:
View Code
1 <Grid x:Name=
"
LayoutRoot
" Background=
"
White
">
2 <Button Content= " 显示子窗口 " Height= " 23 " Click= " ShowWindow_Click " Margin= " 0,138,248,138 " />
3 <sdk:Label Height= " 28 " HorizontalAlignment= " Left " Margin= " 10,10,0,0 " Name= " message " VerticalAlignment= " Top " Width= " 120 " />
4 </Grid>
2 <Button Content= " 显示子窗口 " Height= " 23 " Click= " ShowWindow_Click " Margin= " 0,138,248,138 " />
3 <sdk:Label Height= " 28 " HorizontalAlignment= " Left " Margin= " 10,10,0,0 " Name= " message " VerticalAlignment= " Top " Width= " 120 " />
4 </Grid>
MainPage.xaml.cs:
View Code
1
public
partial
class MainPage : UserControl
2 {
3 MyChildWindow myChildWindow = new MyChildWindow();
4 public MainPage()
5 {
6 InitializeComponent();
7 myChildWindow.Closed += new EventHandler(myChildWindow_Closed);
8 }
9 void myChildWindow_Closed( object sender, EventArgs e)
10 {
11 if (myChildWindow.DialogResult == true)
12 message.Content = string.Format( " 用户名:{0},密码:{1} ", myChildWindow.UserName.Text, myChildWindow.PassWord.Text);
13 else
14 message.Content = "";
15 }
16 // 显示子窗体
17 private void ShowWindow_Click( object sender, RoutedEventArgs e)
18 {
19 myChildWindow.Title = " 显示子窗体 ";
20 myChildWindow.OverlayBrush = new SolidColorBrush(Colors.Yellow);
21 myChildWindow.Opacity = 0.9;
22 myChildWindow.HasCloseButton = true;
23 myChildWindow.Foreground = new SolidColorBrush(Colors.Red);
24 myChildWindow.FontSize = 14;
25 myChildWindow.Show();
26 }
27 }
2 {
3 MyChildWindow myChildWindow = new MyChildWindow();
4 public MainPage()
5 {
6 InitializeComponent();
7 myChildWindow.Closed += new EventHandler(myChildWindow_Closed);
8 }
9 void myChildWindow_Closed( object sender, EventArgs e)
10 {
11 if (myChildWindow.DialogResult == true)
12 message.Content = string.Format( " 用户名:{0},密码:{1} ", myChildWindow.UserName.Text, myChildWindow.PassWord.Text);
13 else
14 message.Content = "";
15 }
16 // 显示子窗体
17 private void ShowWindow_Click( object sender, RoutedEventArgs e)
18 {
19 myChildWindow.Title = " 显示子窗体 ";
20 myChildWindow.OverlayBrush = new SolidColorBrush(Colors.Yellow);
21 myChildWindow.Opacity = 0.9;
22 myChildWindow.HasCloseButton = true;
23 myChildWindow.Foreground = new SolidColorBrush(Colors.Red);
24 myChildWindow.FontSize = 14;
25 myChildWindow.Show();
26 }
27 }
子窗口MyChildWindow.xaml代码
View Code
1 <Grid x:Name=
"
LayoutRoot
" Background=
"
White
">
2 <Grid.ColumnDefinitions>
3 <ColumnDefinition Width= " 100 "/>
4 <ColumnDefinition Width= " 300 "/>
5 </Grid.ColumnDefinitions>
6
7 <Grid.RowDefinitions>
8 <RowDefinition Height= " 50 "/>
9 <RowDefinition Height= " 50 "/>
10 <RowDefinition Height= " 50 "/>
11 </Grid.RowDefinitions>
12
13 <TextBlock Text= " 用户名: " Grid.Column= " 0 " Grid.Row= " 0 " />
14 <TextBox Width= " 200 " x:Name= " UserName " Grid.Column= " 1 " Grid.Row= " 0 " Height= " 30 "/>
15 <TextBlock Text= " 密码: " Grid.Column= " 0 " Grid.Row= " 1 "/>
16 <TextBox Width= " 200 " x:Name= " PassWord " Grid.Column= " 1 " Grid.Row= " 1 " Height= " 30 "/>
17
18 <StackPanel Grid.ColumnSpan= " 2 " Orientation= " Horizontal " HorizontalAlignment= " Center " Grid.Row= " 2 ">
19 <Button Content= " 提交 " Width= " 75 " Height= " 25 " x:Name= " Submit " Click= " Submit_Click "/>
20 <Button Content= " 取消 " Width= " 75 " Height= " 25 " x:Name= " Cancel " Click= " Cancel_Click "/>
21 </StackPanel>
22 </Grid>
2 <Grid.ColumnDefinitions>
3 <ColumnDefinition Width= " 100 "/>
4 <ColumnDefinition Width= " 300 "/>
5 </Grid.ColumnDefinitions>
6
7 <Grid.RowDefinitions>
8 <RowDefinition Height= " 50 "/>
9 <RowDefinition Height= " 50 "/>
10 <RowDefinition Height= " 50 "/>
11 </Grid.RowDefinitions>
12
13 <TextBlock Text= " 用户名: " Grid.Column= " 0 " Grid.Row= " 0 " />
14 <TextBox Width= " 200 " x:Name= " UserName " Grid.Column= " 1 " Grid.Row= " 0 " Height= " 30 "/>
15 <TextBlock Text= " 密码: " Grid.Column= " 0 " Grid.Row= " 1 "/>
16 <TextBox Width= " 200 " x:Name= " PassWord " Grid.Column= " 1 " Grid.Row= " 1 " Height= " 30 "/>
17
18 <StackPanel Grid.ColumnSpan= " 2 " Orientation= " Horizontal " HorizontalAlignment= " Center " Grid.Row= " 2 ">
19 <Button Content= " 提交 " Width= " 75 " Height= " 25 " x:Name= " Submit " Click= " Submit_Click "/>
20 <Button Content= " 取消 " Width= " 75 " Height= " 25 " x:Name= " Cancel " Click= " Cancel_Click "/>
21 </StackPanel>
22 </Grid>
MyChildWindow.xaml.cs代码:
View Code
1
public MyChildWindow()
2 {
3 InitializeComponent();
4 }
5 // 提交
6 private void Submit_Click( object sender, RoutedEventArgs e)
7 {
8 this.DialogResult = true;
9 }
10 // 取消
11 private void Cancel_Click( object sender, RoutedEventArgs e)
12 {
13 this.DialogResult = false;
14 }
2 {
3 InitializeComponent();
4 }
5 // 提交
6 private void Submit_Click( object sender, RoutedEventArgs e)
7 {
8 this.DialogResult = true;
9 }
10 // 取消
11 private void Cancel_Click( object sender, RoutedEventArgs e)
12 {
13 this.DialogResult = false;
14 }
OK,这样就可以弹出子窗口了。