文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>创建子窗体,子窗体向父窗体传值

创建子窗体,子窗体向父窗体传值

时间:2010-10-21  来源:hl3292

项目如下图

 


然后建立我们的界面代码和后台代码如下:
一、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;
        }

    }
}

 

运行效果如下:

 


 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载