Bài viết
Kho tài liệu và bài viết được chia sẻ, đánh giá bởi cộng đồng
Click button chuyển hướng từ MainWindow sang Window2 và đưa text từ window 2 về MainWindow trong WPF
Nội dung bài viết
MainWindow.xaml
<Window x:Class="Test.MainWindow"
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"
xmlns:local="clr-namespace:Test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Button Click="Button_Click">Click</Button>
<TextBlock x:Name="txblData"></TextBlock>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window2 w=new Window2();
w.Show();
//Sử dụng event SendDataWindow2 đã tạo bên Window2.xaml.cs
w.SendDataWindow2 += W_SendDataWindow2;
}
private void W_SendDataWindow2(object sender, Window2EventArgs e)
{
//Hiển thị Data lên TextBlock
txblData.Text = e.Data;
}
}
}
Window2.xaml
<Window x:Class="Test.Window2"
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"
xmlns:local="clr-namespace:Test"
mc:Ignorable="d"
Title="Window2" Height="450" Width="800">
<StackPanel>
<Button Content="Click" Click="Button_Click"></Button>
<TextBox x:Name="txtData"></TextBox>
</StackPanel>
</Window>
Window2.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Test
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
//Tạo event
private event EventHandler<Window2EventArgs> sendDataWindow2;
public event EventHandler<Window2EventArgs> SendDataWindow2
{
add { sendDataWindow2 += value; }
remove { sendDataWindow2 -= value; }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string data=txtData.Text;
//Sử dụng Event sendDataWindow2 để truyền data qua MainWindow
if (sendDataWindow2 != null)
{
sendDataWindow2(this,new Window2EventArgs(data));
}
}
}
//Tạo 1 class event cho riêng mình kể thừa lại EventArgs(Event chuẩn .NET)
public class Window2EventArgs:EventArgs
{
public string Data { get; set; }
public Window2EventArgs(string data)
{
Data = data;
}
}
}
Nội dung bài viết