2009-04-14 4 views

Répondre

10

Si vous connaissez la cellule et la ligne que le contrôle vit, vous pouvez utiliser un LINQ déclaration pour l'attraper.

Voici une déclaration LINQ qui va faire le premier contrôle qui se trouve dans la colonne 3, ligne 4.

var control = (from d in grid.Children 
       where Grid.GetColumn(d as FrameworkElement) == 3 
        && Grid.GetRow(d as FrameworkElement) == 4 
       select d).FirstOrDefault(); 
+0

Nice - encore une fois LINQ supprime le besoin de boucles. –

1

Vous pouvez itérer les enfants de la grille en vérifiant leurs valeurs de ligne et de colonne à l'aide des méthodes Grid.GetRow et Grid.GetColumn et remplacer le contenu ciblé lorsque les valeurs correspondent. Voici un échantillon testé dans WPF, mais devrait fonctionner dans Silverlight:

<Grid x:Name="SampleGrid"> 
    <Grid.RowDefinitions> 
     <RowDefinition /> 
     <RowDefinition /> 
     <RowDefinition /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <Rectangle Fill="Red" Width="20" Height="20" Grid.Row="0" Grid.Column="0" /> 
    <Rectangle Fill="Orange" Width="20" Height="20" Grid.Row="0" Grid.Column="1" /> 
    <Rectangle Fill="Yellow" Width="20" Height="20" Grid.Row="0" Grid.Column="2" /> 
    <Rectangle Fill="Green" Width="20" Height="20" Grid.Row="1" Grid.Column="0" /> 
    <Rectangle Fill="Blue" Width="20" Height="20" Grid.Row="1" Grid.Column="1" /> 
    <Rectangle Fill="Indigo" Width="20" Height="20" Grid.Row="1" Grid.Column="2" /> 
    <Rectangle Fill="Violet" Width="20" Height="20" Grid.Row="2" Grid.Column="0" /> 
    <Rectangle Fill="Black" Width="20" Height="20" Grid.Row="2" Grid.Column="1" /> 
    <Rectangle Fill="Gray" Width="20" Height="20" Grid.Row="2" Grid.Column="2" /> 
    <Button Grid.Row="3" Grid.ColumnSpan="3" Margin="10" x:Name="Swap" Click="Swap_Click" Content="Swap"/> 
</Grid> 

Dans le gestionnaire d'événements:

private void Swap_Click(object sender, RoutedEventArgs e) 
    { 
     Ellipse newEllipse = new Ellipse() { Fill = new SolidColorBrush(Colors.PaleGoldenrod), Width = 20d, Height = 20d }; 
     for (int childIndex = 0; childIndex < this.SampleGrid.Children.Count; childIndex++) 
     { 
      UIElement child = this.SampleGrid.Children[childIndex]; 
      if (Grid.GetColumn(child) == 2 && Grid.GetRow(child) == 2) 
      { 
       this.SampleGrid.Children.Remove(child); 
       Grid.SetRow(newEllipse, 2); 
       Grid.SetColumn(newEllipse, 2); 
       this.SampleGrid.Children.Add(newEllipse); 
      } 
     } 

    } 
+0

aurait dû ajouter que si vous avez un grand nombre ou ligne/Col. vous pouvez ajouter une instruction break pour éviter itérer les autres enfants Une fois que vous avez atteint votre cible. –

0

Vous pouvez rappeler de contrôle que je mets dans la grille d'ajouter une variable privée:

private Control controlCentral = null; 

Ensuite, attribuez à cette variable le contrôle que vous ajoutez à la grille, de sorte que vous pouvez utiliser Supprimer pour supprimer le contrôle de la grille.

Le code suivant remplace le contrôle de la ligne 0 colonne 1:

private void MostrarControlCentral(Control control) 
    { 
     if (control != null) 
     { 
      control.SetValue(Grid.RowProperty, 0); 
      control.SetValue(Grid.ColumnProperty, 1); 
     } 

     this.LayoutRoot.Children.Remove(this.controlCentral); 
     if (control != null) 
     { 
      this.LayoutRoot.Children.Add(control); 
     } 
     this.controlCentral=control; 
    } 
Questions connexes