2010-08-20 3 views
1

나는 Grid 객체를 가지고 있으며 특정 체크 박스를 가져 오려고한다.index 대신 x/y 좌표로 Grid에서 자식에 액세스하는 방법은 무엇입니까?

나는이 구문을 사용할 수 있습니다

CheckBox cbChange = grid.Children[4] as CheckBox; 

을하지만 X를 통해이 아이에 액세스 할 수있는 방법을/y를 대신 좌표 예 :

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); 
     } 
    } 
} 

답변

2

참조 :

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

발췌문 :

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()); 
} 

또는 아래에있는 내 부분적으로 솔루션 :

좌표는 다음 i = rowj = column 경우 :

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

여기에서 x은 그리드에서 행에 포함 할 수있는 항목 수입니다. xgrid.ColumnDifinitions.Count 일 수 있습니다. 그러나 이것은 그리드가 일정한 경우에만 작동합니다.

5

내가 원하는 X-와 y 값을 취하는 확장 메서드를 만들 것이다 (그래서 내가 더 직관적 인 방법이 있는지 모르는 WPF 너무 많은 일을하지 않은 경우). 거기에서 모든 자식을 살펴보고 Grid.GetRow (자식) 및 Grid.GetColumn (자식) 메서드가 원하는 값을 반환하는지 확인합니다.

결과적으로 나는 IEnumerable<FrameworkElement>을 반환 할 것입니다. 왜냐하면이 위치에 하나 이상의 요소가있을 수 있기 때문입니다 (예를 들어 있지 않지만 일반적으로 그러한 방법에 대해). 같은

뭔가 : 나는 그것을 테스트하지 않았습니다

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; 
    } 
} 

, 그것이 작동하지 않는 경우 코멘트를합니다. 하나의 인스턴스 만 반환하려는 경우 코드를 따라 변경할 수 있습니다.

관련 문제