Wednesday, June 15, 2011

WPF - C# grid element by row and column

I have a table with a column filled with a check box for each row and, on the header row, I have a check box that I want to have the "check all" behaviour.

The check box is within a dock panel so that it can be centred on the table cell even over resize.

To do this I have to go trough all table elements and find out my check boxes to set them checked or not checked.

After some time searching fora solution this is what came out:

public void TopCheckBoxChecked(object sender, RoutedEventArgs e)
{
if (mach != null)
{
int row = 1;
int col = 0;

foreach (UIElement element in m_tableGrid.Children)
{
if (Grid.GetColumn(element) == col && Grid.GetRow(element) == row)
{
(((element as DockPanel).Children[0] as UIElement) as CheckBox).IsChecked = (sender as CheckBox).IsChecked;

row++;
}
}
}
}


Not so simple as I would have hopped.

For reference check this blog entry:

Ian Oakes on WPF and Silverlight

No comments: