1.新建项目,选中wpf应用程序。
2.项目中添加 用户控件, tabel1 ;添加用户控件 table2.
Table1 XAML页面显示如下:
<UserControl x:Class="WpfApplication2.table1" // 注意控件的名称
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
Table 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;
using WpfApplication2;
namespace WpfApplication2
{
/// <summary>
/// UserControl1.xaml 的交互逻辑
/// </summary>
public partial class table1 : UserControl
{
public table1()
{
InitializeComponent();
}
}
3.主页面的Xaml 页面:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:table1="clr-namespace:WpfApplication2" // 引用 table1 页面
xmlns:table2="clr-namespace:WpfApplication2" // 引用 table2页面
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl HorizontalAlignment="Left" Height="268" Margin="10,10,0,0" VerticalAlignment="Top" Width="477">
<TabItem Header="table1" Width="50">
<Grid Background="#FFE5E5E5" >
<table1:table1></table1:table1> // 调用table1 页面
</Grid>
</TabItem>
<TabItem Header="table2" Width="50">
<Grid Background="#FFE5E5E5" Margin="0,0,0,-21" >
<table2:table2 Margin="-17,-107,10,90"></table2:table2>
</Grid>
</TabItem>
</TabControl>
Wpf table控件数据及事件的传递:
利用 Gotfocuse 事件。
Main 窗口 XAML内容:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:table1="clr-namespace:WpfApplication2"
xmlns:table2="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid HorizontalAlignment="Left" Height="288" Margin="10,22,0,0" VerticalAlignment="Top" Width="497">
<TabControl HorizontalAlignment="Left" Height="268" Margin="10,10,0,0" VerticalAlignment="Top" Width="477">
<TabItem Header="table1" Width="50" GotFocus="table1_GotFocus" >
<Grid Background="#FFE5E5E5" >
<table1:table1 x:Name="uc_table1" ></table1:table1>
</Grid>
</TabItem>
<TabItem Header="table2" Width="50" GotFocus="table2_GotFocus" >
<Grid Background="#FFE5E5E5" Margin="0,0,0,-21" >
<table2:table2 x:Name="uc_table2" Margin="-17,-107,10,90" ></table2:table2>
</Grid>
</TabItem>
</TabControl>
</Grid>
Main 页面的 cs 传递:
private void table2_GotFocus(object sender, RoutedEventArgs e)
{
uc_table1.table1_loaddate(100,200); // uc_table1 为 table1:tabel 中的x:Name, 在main.cs 页面 就可以调用 table 控件中的方法。
}
private void table1_GotFocus(object sender, RoutedEventArgs e)
{
uc_table1.table1_loaddate(888,999);
}
Table1 控件的cs内容
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication2;
namespace WpfApplication2
{
public partial class table1 : UserControl
{
int g_a; int g_b;
public table1()
{
InitializeComponent();
}
public void table1_loaddate(int a, int b)
{
g_a = a;
g_b = b;
MessageBox.Show("data is "+ g_a + "," +g_b);
}
}
}