如何从MainWindow调用UC中的方法?在我的应用程序中,我使用MainWindow + UserControls + TabControl(一个用户控件=一个tabitem,作为firefox)。当我改变tabitem时,我会在用户控制中调用方法。我将用一个例子来解释这个。
UserControl代码:
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 {}
}
}
MainWindow代码:
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上刷新datagrid。请帮忙
答案 0 :(得分:0)
虽然我建议你改变你在这里创建UC的方法,以便在UC中不依赖于MainWindow但是如果你现在想继续使用它,