tags:

views:

70

answers:

2

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

alt text

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);
        }
    }
}
+1  A: 

See:

http://www.trillian.com.au/2009/01/how-to-get-element-from-grid-using-xy.html

Snippet:

public static Collection<TElement> GetElements<TElement>(this Grid grid, int row, int column) 
   where TElement : UIElement 
{ 
   var elements = from UIElement element in grid.Children 
                  where element is TElement && 
                        Grid.GetRow(element) == row && 
                        Grid.GetColumn(element) == column 
                  select element as TElement; 
   return new Collection<TElement>(elements.ToList()); 
} 

or my partially solution below:

If your coordinates is i = row and j = column then:

CheckBox cbChange = grid.Children[i * x + j - 1] as CheckBox;

where x is how many items a row can contain in your grid. x could be grid.ColumnDifinitions.Count. This, however, will only work if you your grid is uniform.

(I have not worked so much with WPF so I don't know whether there is a more intuitive way)

lasseespeholt
+5  A: 

I would make an extension method that takes the desired x-and y-values. There in you go through all children and check if the Grid.GetRow(child) and Grid.GetColumn(child)-methods return the desired values.

As a result I would return an IEnumerable<FrameworkElement> because you can have more than one, one or no elements at this position (not in your example, but for such a method in general).

Something like:

public static class GridExtensions {
    public static IEnumerable<FrameworkElement> GetXYChild(this Grid instance, int x, int y) {
        if (null == instance) {
            throw new ArgumentNullException("instance");
        }
        List<FrameworkElement> list = new List<FrameworkElement>();            
        foreach (FrameworkElement fe in instance.Children) {
            if (Grid.GetRow(fe) == y && Grid.GetColumn(fe) == x) {
                list.Add(fe);
            }
        }
        return list;
    }
}

I have not tested it, make a comment if it does not work. If you only want to return one instance, you can change the code acordingly.

HCL