Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
KeyValueGrid.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Windows;
5 using System.Windows.Controls;
6 
7 namespace SiliconStudio.Presentation.Controls
8 {
9  /// <summary>
10  /// This control represents a <see cref="Grid"/> with two columns, the first one representing keys and the second one representing
11  /// values. <see cref="Grid.ColumnDefinitions"/> and <see cref="Grid.RowDefinitions"/> should not be modified for this control. Every
12  /// child content added in this control will either create a new row and be placed on its left column, or placed on the second column
13  /// of the last row.
14  /// </summary>
15  /// <remarks>The column for the keys has an <see cref="GridUnitType.Auto"/> width.</remarks>
16  /// <remarks>The column for the values has an <see cref="GridUnitType.Star"/> width.</remarks>
17  public class KeyValueGrid : Grid
18  {
19  private bool gridParametersInvalidated;
20 
21  /// <summary>
22  /// Initializes a new instance of the <see cref="KeyValueGrid"/> class.
23  /// </summary>
24  public KeyValueGrid()
25  {
26  ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.0, GridUnitType.Auto) });
27  ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0, GridUnitType.Star) });
28  }
29 
30  /// <summary>
31  /// Recomputes rows and update children.
32  /// </summary>
33  private void InvalidateGridParameters()
34  {
35  RowDefinitionCollection rowCollection = RowDefinitions;
36  UIElementCollection children = Children;
37 
38  // Determine how many rows we need
39  int remainder;
40  int neededRowCount = Math.DivRem(children.Count, 2, out remainder) + remainder;
41 
42  int currentRowCount = rowCollection.Count;
43  int deltaRowCount = neededRowCount - currentRowCount;
44 
45  // Add/remove rows
46  if (deltaRowCount > 0)
47  {
48  for (int i = 0; i < deltaRowCount; i++)
49  rowCollection.Add(new RowDefinition { Height = new GridLength(0.0, GridUnitType.Auto) });
50  }
51  else if (deltaRowCount < 0)
52  {
53  rowCollection.RemoveRange(currentRowCount + deltaRowCount, -deltaRowCount);
54  }
55 
56  // Update Grid.Row and Grid.Column dependency properties on each child control
57  int row = 0;
58  int column = 0;
59  foreach (UIElement element in children)
60  {
61  element.SetValue(ColumnProperty, column);
62  element.SetValue(RowProperty, row);
63 
64  column++;
65  if (column > 1)
66  {
67  column = 0;
68  row++;
69  }
70  }
71  }
72 
73  /// <inheritdoc/>
74  protected override Size MeasureOverride(Size constraint)
75  {
76  if (gridParametersInvalidated)
77  {
78  gridParametersInvalidated = false;
79  InvalidateGridParameters();
80  }
81 
82  return base.MeasureOverride(constraint);
83  }
84 
85  /// <inheritdoc/>
86  protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
87  {
88  gridParametersInvalidated = true;
89  base.OnVisualChildrenChanged(visualAdded, visualRemoved);
90  }
91  }
92 }
override Size MeasureOverride(Size constraint)
Definition: KeyValueGrid.cs:74
override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
Definition: KeyValueGrid.cs:86
KeyValueGrid()
Initializes a new instance of the KeyValueGrid class.
Definition: KeyValueGrid.cs:24
This control represents a Grid with two columns, the first one representing keys and the second one r...
Definition: KeyValueGrid.cs:17