在 UserControl 按钮单击事件中从父 Window 调用 public void 方法
Calling public void method from Parent Window in UserControl button click event
我的 MainWindow 导航菜单中有一个 label
,内容绑定到 SQL 命令,该方法称为 public void HBD_Count()
我有多个 UserControls
,在 Button_Click
上我想调用 public void HBD_Count()
以便可以刷新 label
中的值。
我尝试从我的主Window 页面调用 public partial class
到我的 UserControl
页面,但它不起作用。
这是我的 Main Window 中的代码,它填充 label
:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MyWindow_Loaded;
HBD_Count();
}
public void HBD_Count()
{
try
{
SqlConnection connection = new SqlConnection("Data Source=WINDOWS-B1AT5HC\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
string selectQuery = ("SELECT COUNT(*) AS HBDCount FROM hb_Disputes WHERE (ASSGNTO = 'E099255') AND (STATUS = 3)");
connection.Open();
SqlCommand command = new SqlCommand(selectQuery, connection);
SqlDataReader sqlReader = command.ExecuteReader();
while (sqlReader.Read())
{
HBD_Counts.Content = sqlReader["HBDCount"].ToString();
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
这是我的 UserControl
页面中的代码:
public partial class Import_HighBill : UserControl
{
public string ValueString { get; set; }
public partial class MainWindow : Window { }
public Import_HighBill()
{
InitializeComponent();
AssignList();
}
private void butn_Assign_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=WINDOWS-B1AT5HC\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
try
{
SqlCommand cmd = new SqlCommand("UPDATE [hb_Disputes] SET ASSGNTO=@ASSGNTO WHERE DSP_ID=@DSP_ID", con);
cmd.Parameters.AddWithValue("@DSP_ID", txt_ID.Text);
// cmd.Parameters.AddWithValue("@DATERSLVD", DBNull.Value);
// Analyst Name
if (cmb_AnalystName.SelectedValue == null)
{
cmd.Parameters.AddWithValue("@ASSGNTO", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@ASSGNTO", cmb_AnalystName.SelectedValue);
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
// Here is where I am trying to call the method
HBD_Count();
// Clear Search Fields
cmb_AnalystName.SelectedIndex = -1;
MessageBox.Show("Dispute Assinged!!!");
AssignList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
您可以使用以下代码来完成此任务:
Window parentWindow = Window.GetWindow(this);
((MainWindow)parentWindow).HBD_Count();
如果您需要调用不同的方法,MVVM 概念可以简化任务。在这种情况下,您需要将方法调用包含在 Command and will be able to access it using a RelativeSource binding 中。
我的 MainWindow 导航菜单中有一个 label
,内容绑定到 SQL 命令,该方法称为 public void HBD_Count()
我有多个 UserControls
,在 Button_Click
上我想调用 public void HBD_Count()
以便可以刷新 label
中的值。
我尝试从我的主Window 页面调用 public partial class
到我的 UserControl
页面,但它不起作用。
这是我的 Main Window 中的代码,它填充 label
:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MyWindow_Loaded;
HBD_Count();
}
public void HBD_Count()
{
try
{
SqlConnection connection = new SqlConnection("Data Source=WINDOWS-B1AT5HC\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
string selectQuery = ("SELECT COUNT(*) AS HBDCount FROM hb_Disputes WHERE (ASSGNTO = 'E099255') AND (STATUS = 3)");
connection.Open();
SqlCommand command = new SqlCommand(selectQuery, connection);
SqlDataReader sqlReader = command.ExecuteReader();
while (sqlReader.Read())
{
HBD_Counts.Content = sqlReader["HBDCount"].ToString();
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
这是我的 UserControl
页面中的代码:
public partial class Import_HighBill : UserControl
{
public string ValueString { get; set; }
public partial class MainWindow : Window { }
public Import_HighBill()
{
InitializeComponent();
AssignList();
}
private void butn_Assign_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=WINDOWS-B1AT5HC\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
try
{
SqlCommand cmd = new SqlCommand("UPDATE [hb_Disputes] SET ASSGNTO=@ASSGNTO WHERE DSP_ID=@DSP_ID", con);
cmd.Parameters.AddWithValue("@DSP_ID", txt_ID.Text);
// cmd.Parameters.AddWithValue("@DATERSLVD", DBNull.Value);
// Analyst Name
if (cmb_AnalystName.SelectedValue == null)
{
cmd.Parameters.AddWithValue("@ASSGNTO", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@ASSGNTO", cmb_AnalystName.SelectedValue);
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
// Here is where I am trying to call the method
HBD_Count();
// Clear Search Fields
cmb_AnalystName.SelectedIndex = -1;
MessageBox.Show("Dispute Assinged!!!");
AssignList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
您可以使用以下代码来完成此任务:
Window parentWindow = Window.GetWindow(this);
((MainWindow)parentWindow).HBD_Count();
如果您需要调用不同的方法,MVVM 概念可以简化任务。在这种情况下,您需要将方法调用包含在 Command and will be able to access it using a RelativeSource binding 中。