分享

通过代码查找Grid中在后台手动添加的控件

 xyjackxjw 2013-05-14

通过代码查找Grid中在后台手动添加的控件

时间:2011-10-16 06:32来源:百度空间 作者:lilipangtou 点击: 388次
Grid Grid.RowDefinitions RowDefinitionHeight= 100 / RowDefinitionHeight= 35 / /Grid.RowDefinitions Grid.ColumnDefinitions /Grid.ColumnDefinitions Button x:Name= btnClick Margin= 50,0,50,0 Grid.Row= 1 Grid.Column= 0 Content= ClickMe Click= btnClick_Click /
  
<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="35"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
        </Grid.ColumnDefinitions>
        <Button x:Name="btnClick" Margin="50,0,50,0" Grid.Row="1" Grid.Column="0" Content="Click Me" Click="btnClick_Click"/>
        <Grid Grid.Row="0" Grid.Column="0" x:Name="Grid1">
        </Grid>
    </Grid>

 

public Window1()
        {
            InitializeComponent();
            this.Loaded +=new RoutedEventHandler(Window1_Loaded);
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            RowDefinition rd =new RowDefinition();
            Grid1.RowDefinitions.Add(rd);
            //添加第0列
            Border b =new Border();
            b.BorderThickness =new Thickness(0, 0, 1, 1);
            b.BorderBrush = Brushes.Black;
            b.Name ="Border_"+ (Grid1.RowDefinitions.Count -1) +"_0";

            TextBox tb =new TextBox();
            string x ="TB_"+ (Grid1.RowDefinitions.Count -1) +"_0";
            tb.Name ="TB_"+ (Grid1.RowDefinitions.Count -1) +"_0";
            tb.BorderThickness =new Thickness(0);
            tb.ToolTip = tb.Name;
            b.Child = tb;//
            b.SetValue(Grid.RowProperty, Grid1.RowDefinitions.Count -1);
            b.SetValue(Grid.ColumnProperty, 0);
            Grid1.Children.Add(b);

        }

        privatevoid btnClick_Click(object sender, RoutedEventArgs e)
        {
            Border border;
            TextBox tb;
            for (int i =0; i < Grid1.RowDefinitions.Count; i++)
            {
                // 这个方法是循环Grid下所有控件的方法
                //FindName 方法只能查询在XAML中定义的组件,后台动态添加的需要手动写循环来处理
                for (int j =0; j < Grid1.Children.Count; j++)
                {
                    border = Grid1.Children[i] as Border;
                    if (border !=null&& border.Name =="Border_"+ i +"_0")
                    {
                        tb = border.Child as TextBox;
                        if (tb !=null&& tb.Name =="TB_"+ i +"_0")
                        {
                            if (tb.Text.Trim().Equals(""))
                            {
                                MessageBox.Show("第"+ (i +1) +"行的摘要不能为空");
                            }

                        }
                    }
                }
            }
        }

  如果是在xaml中添加,应用FindName

  本文来自lilipangtou的博客,原文地址:http://hi.baidu.com/lilipangtou/blog/item/0e2653ccf0dd8a21f8dc6131.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多