[英]Calling public void method from Parent Window in UserControl button click event
I have a label
in my MainWindow navigation menu, and the content is binding to SQL command and that method is called public void HBD_Count()
我的 MainWindow 导航菜单中有一个
label
,内容绑定到 SQL 命令,该方法称为public void HBD_Count()
I have various UserControls
where on a Button_Click
I would like to call public void HBD_Count()
so that value in my label
can be refreshed.我有各种
UserControls
,在Button_Click
上我想调用public void HBD_Count()
以便可以刷新label
中的值。
I tried called the public partial class
from my MainWindow to my UserControl
pages but it does not work.我尝试将
public partial class
从我的 MainWindow 调用到我的UserControl
页面,但它不起作用。
Here is my code in my Main Window, which populates the label
:这是我在主 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());
}
}
Here is my code in my UserControl
page:这是我的
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);
}
}
You can use the following code to accomplish this task:您可以使用以下代码来完成此任务:
Window parentWindow = Window.GetWindow(this);
((MainWindow)parentWindow).HBD_Count();
The MVVM concept allows simplifying the task if you need to call different various methods.如果您需要调用不同的各种方法,MVVM 概念可以简化任务。 In this case you will need to include the method call inside the Command and will be able to access it using a RelativeSource binding .
在这种情况下,您需要在Command中包含方法调用,并且能够使用RelativeSource 绑定来访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.