问题描述
我通过创建自定义inkcanvas类创建了一个inkcanvas控件。我已经获取了这个类的实例并将它们添加到堆栈面板。我将一个按钮控件拖到主窗口。在按钮单击事件中,stackpanel名称的实例(mycanvas1)不明显。如何解决它。
这里我的代码
I created a inkcanvas control by creating a custom inkcanvas class .I had taken the instances of this class and add them to stack panel.I dragged the one button control to the mainwindow.In the button click event the instance of stackpanel names (mycanvas1 ) was not visible.how to solve it.
here my code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
mycan mycanvas1 = new mycan();
stkm.Children.Add(mycanvas1);
}
private void savebtn_Click(object sender, RoutedEventArgs e )
{
var files = Directory.GetFiles(@"C:\\Users\\SriRampavankumar\\Desktop\\proj").Length;
string fileName = namebox.Text.Substring(0, 3) + -+ ++files + ".txt";
string path2 = System.IO.Path.GetFullPath("C:\\Users\\tom\\Desktop\\proj");
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string var = System.IO.Path.Combine(docPath, path2);
string var1 = System.IO.Path.Combine(var, fileName);
using (StreamWriter writer = new StreamWriter(var1))
{
string var6 = namebox.Text;
string var7 = " ";
string var8 = agebox.Text;
}
推荐答案
private childItem FindVisualChild<childitem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childitem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
并按下按钮点击代码 -
and call following code on your button click -
object obj = FindVisualChild<mycan>(stkm);
如果要在stakpanel中添加多个子项,则为每个子项提供Name,并在FindVisualChild函数中使用Name属性进行检查。
If you will add more than one child items into the stakpanel then give "Name" to each child item and check inside "FindVisualChild" function with "Name" property.
这篇关于如何在按钮单击事件中调用stackpanel子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!