![](/img/trans.png)
[英]Updating WPF progress bar value inside a user control from another method in a separate class?
[英]How run a method inside a wpf user control (something.xaml.cs) from another class?
假設您有一個用戶控件,其中有一種方法可以重置表格:
public void myReset()
{
text1.Text="";
text2.Text="";
}
現在我想從clsMyClass中的方法調用此myReset()方法
class clsMyclass
{
public clsMyClass()
{
}
// ==================== Methods =================
public double SomeMethod(double Val)
{
UserControlRef.myReset();
//Do things...
}
}
我不知道如何創建對用戶控件的引用。 我已經看到使用Revit Extension的代碼以這種方式工作。 我已經進行了很多搜索來找出答案,但是我找不到正確的方法。
您應該能夠在xaml中設置x:Name屬性: x:Name="control"
這將為您的實例提供一個變量名。 然后,在代碼中,您可以將實例稱為您為其指定的名稱。 例如control.myReset();
根據@StijnvanGaal的說法,它不是一個非常好的方法,無論如何,您都可以檢索任何UserControl及其內容。這里有一個示例,您的用戶控件位於grid2中,您可以通過這種方式訪問它。
int ChildNumber = VisualTreeHelper.GetChildrenCount(grid2);
for (int i = 0; i < ChildNumber; i++)
{
Control v = (Control)VisualTreeHelper.GetChild(grid2, i);
if (v.GetType().ToString() == "Project_wpf.UserControlRef")
{
UserControlRef CM = v as UserControlRef;
Console.WriteLine(CM.Name); //you can check his name here
CM.myReset();
}
}
基本上,這將在Grid2的子級的所有“ Project_wpf.UserControlRef”類型的UserControle中激活MyReset()方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.