I have a Grid object and want to get a specific checkbox out of it.
I can use this syntax:
CheckBox cbChange = grid.Children[4] as CheckBox;
But how can I access this child via x/y coordinate instead, e.g.:
CheckBox cbChange = grid.GetXYChild(2,3) as CheckBox; //PSEUDO-CODE
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace TestGrid92292
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<string> rowNames = new List<string>
{
"Marketing",
"Sales",
"Development"
};
Grid grid = new Grid();
grid.Margin = new Thickness(5);
ColumnDefinition col1 = new ColumnDefinition();
col1.Width = new GridLength(100, GridUnitType.Pixel);
grid.ColumnDefinitions.Add(col1);
ColumnDefinition col2 = new ColumnDefinition();
col2.Width = new GridLength(1, GridUnitType.Star);
grid.ColumnDefinitions.Add(col2);
ColumnDefinition col3 = new ColumnDefinition();
col3.Width = new GridLength(1, GridUnitType.Star);
grid.ColumnDefinitions.Add(col3);
int rowCount = 0;
foreach (var rowName in rowNames)
{
RowDefinition row = new RowDefinition();
grid.RowDefinitions.Add(row);
TextBlock tb = new TextBlock();
tb.Text = rowName;
tb.SetValue(Grid.ColumnProperty, 0);
tb.SetValue(Grid.RowProperty, rowCount);
grid.Children.Add(tb);
CheckBox cb = new CheckBox();
cb.SetValue(Grid.ColumnProperty, 1);
cb.SetValue(Grid.RowProperty, rowCount);
cb.HorizontalAlignment = HorizontalAlignment.Left;
grid.Children.Add(cb);
CheckBox cb2 = new CheckBox();
cb2.SetValue(Grid.ColumnProperty, 2);
cb2.SetValue(Grid.RowProperty, rowCount);
cb2.HorizontalAlignment = HorizontalAlignment.Left;
grid.Children.Add(cb2);
rowCount++;
}
//check a specific box
//CheckBox cbChange = grid.GetXYChild(2,3) as CheckBox;
CheckBox cbChange = grid.Children[4] as CheckBox;
cbChange.IsChecked = true;
MainContent.Children.Add(grid);
}
}
}