2015-04-30 1 views
0

J'ai écrit cette méthode environ 3 fois maintenant et je suis certain que je ne vais pas faire écrire pendant un certain temps:Filler Grille générique Extension Méthode

public void PopulateField() { 
     this.grdNameField.ColumnDefinitions.Clear(); 
     this.grdNameField.RowDefinitions.Clear(); 
     this.grdNameField.Children.Cast<Viewbox>().ToList().ForEach(VB => VB.Child = null); 
     this.grdNameField.Children.Clear(); 

     for (int x = 0; x < Settings.Default.PlayerCount; x++) { 
      Viewbox VB = new Viewbox() { Child = this.PlayerNamers[x], Stretch = Stretch.Uniform }; 
      this.grdNameField.Children.Add(VB); 
      Grid.SetColumn(VB, x % Math.Max(3, (int)(Math.Ceiling(Settings.Default.PlayerCount/5.0D)))); 
      Grid.SetRow(VB, x/Math.Max(3, (int)(Math.Ceiling(Settings.Default.PlayerCount/5.0D)))); 
     } 

     int 
      ColumnCount = this.grdNameField.Children.Cast<Viewbox>().Max(VB => Grid.GetColumn(VB)) + 1, 
      RowCount = this.grdNameField.Children.Cast<Viewbox>().Max(VB => Grid.GetRow(VB)) + 1; 

     for (int x = 0; x < ColumnCount; x++) 
      this.grdNameField.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); 

     for (int x = 0; x < RowCount; x++) 
      this.grdNameField.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); 
    } 

J'ai enfin remarqué que la différence entre les trois fonctions est seulement ce que la grille est remplie: Une liste générique d'objets Liste (et à l'occasion, à quoi la propriété Viewbox Stretch est définie).

Je veux écrire une méthode générique pour remplacer ces trois (et toutes les futures) méthodes et j'ai une idée de comment le faire ...

Répondre

-1

Comme je tapais cette question, il m'a semblé que je pouvais aller plus loin que simplement écrire une méthode générique pour gérer peuplant Grids - Je pourrais écrire une méthode d'extension, et c'est ce que je faisais:

public static void Populate<T>(this Grid Parent, List<T> Children, Stretch ViewBoxStretch) where T : UIElement { 
     Parent.ColumnDefinitions.Clear(); 
     Parent.RowDefinitions.Clear(); 
     Parent.Children.Cast<Viewbox>().ToList().ForEach(VB => VB.Child = null); 
     Parent.Children.Clear(); 

     for (int x = 0; x < Children.Count; x++) { 
      Viewbox VB = new Viewbox() { Child = Children[x], Stretch = ViewBoxStretch }; 
      Parent.Children.Add(VB); 
      Grid.SetColumn(VB, x % Math.Max(3, (int)(Math.Ceiling(Children.Count/5.0D)))); 
      Grid.SetRow(VB, x/Math.Max(3, (int)(Math.Ceiling(Children.Count/5.0D)))); 
     } 

     int 
      ColumnCount = Parent.Children.Cast<Viewbox>().Max(VB => Grid.GetColumn(VB)) + 1, 
      RowCount = Parent.Children.Cast<Viewbox>().Max(VB => Grid.GetRow(VB)) + 1; 

     for (int x = 0; x < ColumnCount; x++) 
      Parent.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); 

     for (int x = 0; x < RowCount; x++) 
      Parent.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); 
    } 

Rappelez-vous, pas tout le monde voudra jeter leurs enfants dans des boîtes, et probablement ce (ou quelque chose de suffisamment proche a déjà été fait), mais j'ai pensé que je voudrais partager cela.