【发布时间】:2015-04-14 09:20:06
【问题描述】:
如何从 MainWindow 调用 UC 中的方法?在我的应用程序中,我使用 MainWindow + UserControls + TabControl(一个用户控件 = 一个 tabitem,作为 firefox)。当我更改 tabitem 时,我会在用户控件中调用方法。我会用一个例子来解释这一点。
用户控制代码:
MainWindow mw;
public userControl(MainWindow main)
{
this.mw = main;
InitializeComponent();
LoadData();
}
public void LoadData()
{
using (var mod = new Data())
{
try
{
var query = (from c in mod.table
select c).ToList();
dataGrid.ItemsSource = null;
dataGrid.ItemsSource = query;
}
catch {}
}
}
主窗口代码:
public void RefreshUserControl(string userControlName)
{
switch (userControlName)
{//... others UC
// ....
case "userControl":
userControl ue = new userControl(this);
ue.LoadData();
break;
}
}
private void tabUC_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = sender as TabControl;
var selected = item.SelectedItem as TabItem;
if (selected != null)
{
RefreshUserControl(selected.Name.ToString());
}
}
通常,示例 - 当我更改“userControl”上的 tabitem 时,然后刷新此 usercontrol 上的数据网格。请帮忙
【问题讨论】:
标签: c# wpf user-controls