zoukankan      html  css  js  c++  java
  • 创建子窗体,子窗体向父窗体传值,父窗体向子窗体传值

    先解决父窗体向子窗体传值 ,呵呵,父窗体中创建子窗体时通过构造函数传值。

    解决剩余的:

    此处我们将使用Silverlight3来创建一个子窗口,我们要达到的目标包括:
       1、子窗口从父窗口中获取信息并显示在子窗口的标题栏
       2、设置当进入子窗口时取得焦点的控件
       3、父窗口获取子窗口的相关信息包括按钮信息和文本输入框信息
    实现过程如下:
    首先新建项目SLChildWindow,在此项目中加入Silverligth的SLChildWindow控件,加入方式如下图

    项目如下图


    然后建立我们的界面代码和后台代码如下:
    一、MainPage.xaml与MainPage.xaml.cs代码如下

    MainPage.xaml代码

    <UserControl x:Class="SLChildWindow.MainPage"
        xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d
    ="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc
    ="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable
    ="d"
        d:DesignHeight
    ="188" d:DesignWidth="323" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" FontSize="12" FontWeight="Bold">
        
    <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Left" Height="300" Width="400">
            
    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10">
                
    <Button Content="按钮一" Click="Button_Click1" Margin="10" HorizontalAlignment="Left"/>
                
    <Button Content="按钮二" Click="Button_Click2" Margin="10" HorizontalAlignment="Left"/>
                
    <TextBlock x:Name="dialogResult" Text="子窗口按下的按钮信息显示" Margin="10" Foreground="#FF4420E8" />
            
    </StackPanel>
            
    <dataInput:Label Height="22" Margin="150,150,96,0" Name="lblReturnTxt" VerticalAlignment="Top" Foreground="#FFFF2D3E" Background="#FFF8F8F8" />
            
    <dataInput:Label Height="22" HorizontalAlignment="Left" Margin="20,150,0,0" Name="label1" VerticalAlignment="Top" Width="116" Content="从子窗口取回的数据:" />
        
    </Grid>
    </UserControl>

    MainPage.xaml.cs代码 

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SLChildWindow
    {
        
    public partial class MainPage : UserControl
        {
            
    private PopupWindow popupWindow;

            
    public MainPage()
            {
                InitializeComponent();
                popupWindow 
    = new PopupWindow();
                popupWindow.Closed 
    += new EventHandler(PopupWindow_Closed);
            }


            
    private void PopupWindow_Closed(object sender, EventArgs e)
            {
                
    if ((bool)popupWindow.DialogResult)
                {
                    dialogResult.Text 
    = "用户在子窗口按下的是确定按钮.";
                    lblReturnTxt.Content 
    = popupWindow.txtInput.Text.ToString();
                }
                
    else
                {
                    dialogResult.Text 
    = "用户在子窗口按下的是取消按钮";
                }
            }

            
    private void Button_Click1(object sender, RoutedEventArgs e)
            {
                popupWindow.Title 
    = "按下按钮一";
                popupWindow.SetMessage(
    "你按下的是按钮一.");
                popupWindow.txtInput.Text 
    = "";
                popupWindow.Show();
            }

            
    private void Button_Click2(object sender, RoutedEventArgs e)
            {
                popupWindow.Title 
    = "按下按钮二";
                popupWindow.SetMessage(
    "你按下的是按钮二.");
                popupWindow.txtInput.Text 
    = "";
                popupWindow.Show();
            }
        }

        }

    二、PopupWindow.xaml与PopupWindow.xaml.cs代码如下

    PopupWindow.xaml代码

    代码
    <controls:ChildWindow x:Class="SLChildWindow.PopupWindow"
               xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:controls
    ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
               Width
    ="400" Height="300"
               Title
    ="PopupWindow" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input">
        
    <Grid x:Name="LayoutRoot" Margin="2">
            
    <Grid.RowDefinitions>
                
    <RowDefinition />
                
    <RowDefinition Height="Auto" />
            
    </Grid.RowDefinitions>
            
    <TextBlock x:Name="Message" Text="" TextWrapping="Wrap"/>
            
            
    <Button x:Name="CancelButton" Content="取消" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
            
    <Button x:Name="OKButton" Content="确定" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
           
            
    <dataInput:Label Height="20" HorizontalAlignment="Left" Margin="46,48,0,0" Name="label1" VerticalAlignment="Top" Width="100" Content="请输入文本" />
            
    <TextBox  x:Name="txtInput"  Height="60" HorizontalAlignment="Left" Margin="46,82,0,0"  VerticalAlignment="Top" Width="280" />
        
    </Grid>
    </controls:ChildWindow>

    PopupWindow.xaml.cs代码

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SLChildWindow
    {
        
    public partial class PopupWindow : ChildWindow
        {
            
    public PopupWindow()
            {
                      
                InitializeComponent();
                
    this.Loaded += new RoutedEventHandler(PopupWindow_Loaded);
            }

            
    void PopupWindow_Loaded(object sender, RoutedEventArgs e)
            {
                
    this.GotFocus += new RoutedEventHandler(PopupWindow_GotFocus);    
            }

            
    void PopupWindow_GotFocus(object sender, RoutedEventArgs e)
            {
               txtInput.Focus();
               GotFocus 
    -= PopupWindow_GotFocus;
            }

            
    public void SetMessage(string theMessage)
            {
                Message.Text 
    = theMessage;

            }

            
    private void OKButton_Click(object sender, RoutedEventArgs e)
            {
                
    this.DialogResult = true;
                
            }

            
    private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
                
    this.DialogResult = false;
            }

        }
    }

    运行效果如下:


     

  • 相关阅读:
    overflow 溢出
    float1
    AI赋能测试_API测试
    AI赋能测试_APP测试智能化
    最最最基础题应知应会题目_1_排序_下载图片
    AI赋能测试_遗传算法应用
    PAI使用方法
    nlu模型测试集构建语料多样性
    机器学习基础功能练习II
    python机器学习数据绘图总结
  • 原文地址:https://www.cnblogs.com/hl3292/p/1857580.html
Copyright © 2011-2022 走看看