摘要:

在Grid中编程添加控件,布局控件的位置用到的代码是:

                gridLayout.Children.Add(textblock);
Grid.SetRow(textblock, );
Grid.SetColumn(textblock, );

以上代码是把新创建的名称为textblock的TextBlock 控件添加到gridLayout的Grid控件中,放到第0行第1列。编程添加控件需要注意的就是一定要给新创建的控件一个符合命名规范的名称,这样在查找该控件修改其属性的时候会方便很多。在gridLayout中按名称查找TextBlock控件的方法:

foreach (object child in gridLayout.Children)
{
if (child is TextBox)
{
TextBox textBox = child as TextBox;
if (textBox.Name == name)
return textBox;
}
}

需要注意的是,gridLayout.Children 是所有直接添加到gridLayout中的控件, 并不是所有后代控件,例如,将StackPanel添加到gridLayout中, StackPanel中的所有控件包括TextBlock不能在gridLayout.Children中找到。

 例子:

举个例子,生成一个4行5列的棋盘, 每个单元格中安放一个TextBlock控件和Button控件。单击Button控件将TextBlock的Name显示在TextBlock中。

XAML中定义gridLayout的Grid:

        <Grid x:Name="gridLayout" >
</Grid>

生成Rows 和Columns:

        public void LoadGridTable()
{
int[] Row = new int[] {, , , };
int[] Column = new int[] {,,,, }; gridLayout.ShowGridLines = true;
for(int i =; i< Row.Length; i++)
{
gridLayout.RowDefinitions.Add(new RowDefinition
{
Height = new GridLength()
});
} for(int j=; j< Column.Length; j++)
{
gridLayout.ColumnDefinitions.Add(new ColumnDefinition
{
Width = new GridLength()
});
}
}

在LoadGridTable()中,添加一个StackPanel到gridLayout中:

        。。。

        // Add TextBlock and Button
for(int i=; i< Column.Length; i++)
{
for(int j=; j<Row.Length; j++)
{
StackPanel stackPanel = new StackPanel();
// TODO: Add TextBlock and Button to the stackpanel
gridLayout.Children.Add(stackPanel);
Grid.SetRow(stackPanel, j);
Grid.SetColumn(stackPanel, i);
}
}

插入TextBox 和Button到StackPanel中,注意TextBox 和Button 的Name属性的命名规范:

                    StackPanel stackPanel = new StackPanel();
// TODO: Add TextBlock and Button to the stackpanel
stackPanel.Orientation = Orientation.Horizontal;
TextBox textBox = new TextBox();
textBox.Name = $"lbl_{j}_{i}";
textBox.Text = "NULL"; // Style
textBox.Width = ;
textBox.IsEnabled = false;
stackPanel.Children.Add(textBox); Button button = new Button();
button.Name = $"btn_{j}_{i}";
button.Content = "Tab";
button.Margin = new Thickness(, , , );
button.Click += Button_Click1;
button.Width = ;
stackPanel.Children.Add(button); gridLayout.Children.Add(stackPanel);

Button_Click1 以及按名称查找TextBox的处理方法:

        private void Button_Click1(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
var name = btn.Name;
string[] identityIDs = name.Split(new char[] { '_' }); var textBoxName = $"lbl_{identityIDs[1]}_{identityIDs[2]}"; TextBox siblingTextBox = (TextBox)FindTextBoxByName(textBoxName);
siblingTextBox.Text = textBoxName.ToString();
} private object FindTextBoxByName(string name)
{
foreach (var child in gridLayout.Children)
{
if(child is StackPanel)
{
foreach (var spChild in ((StackPanel)child).Children)
{
if(spChild is TextBox)
{
if(((TextBox)spChild).Name == name)
{
return spChild;
}
}
}
}
}
return null;
}

当点击某个Button时, 在Button_Click1中,找到被点击Button的Name,解析出它所在的行和列,然后依据命名规范,拼接出相应TextBox的Name,按照Name去匹配gridLayout中的StackPanel下的所有TextBox,找到以后,改变TextBox的Text的值,从NULL修改成相应的Name的值。

最新文章

  1. 利用Python进行数据分析(4) NumPy基础: ndarray简单介绍
  2. 【HOW】SharePoint如何彻底删除用户
  3. HTTP请求头参数
  4. 该如何理解AMD ,CMD,CommonJS规范--javascript模块化加载学习总结
  5. 获得项目的绝对地址 getRequestURI,getRequestURL的区别
  6. sqlldr导入数据
  7. [Swift 语法点滴]—— Struct Vs Class
  8. c++ 联合体
  9. Java 方法重载,方法重写(覆盖),继承等细节注意
  10. 前端工程之CDN部署
  11. android的Binder通信机制java层浅谈-android学习之旅(88)
  12. 深入Redis持久化
  13. Elastic 今日在纽交所上市,股价最高暴涨122%。
  14. wxPython:消息对话框MessageDialog
  15. 常见的CSS属性和值CascadingStyleSheets
  16. offsetof与container_of宏[总结]
  17. 【Mac】安装MAMP的PHPredis扩展
  18. 基于Arch的live系统
  19. poj2082单调栈
  20. 【Java】Java_11运算符

热门文章

  1. vue-element-ui之弹窗重置
  2. 一个springboot注解不成功的小问题
  3. 【Redfin SDE intern】跪经
  4. (03) spring Boot 的配置
  5. Gradient Boosting, Decision Trees and XGBoost with CUDA ——GPU加速5-6倍
  6. Source Insight 4.0安装使用教程
  7. day 10 函数命名空间、函数嵌套和作用域
  8. CentOS7系统上的GPSTK示例代码调试 &amp; 运行结果 &amp; 心得
  9. Angular2+ 实现组件交互的众多方式
  10. python3 发送gzip文件请求