如何在按钮单击事件中调用stackpanel子项 [英] How to call the stackpanel children in button click events

查看:78
本文介绍了如何在按钮单击事件中调用stackpanel子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过创建自定义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;

           }

推荐答案

Hi Pavan



您可以使用此功能在StackPanel中找到子项

http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname%28v=vs .110%29.aspx [ ^ ]



Hi Pavan

You can find child from StackPanel with this function which is given here
http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname%28v=vs.110%29.aspx[^]

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子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆