2009-04-14 18 views

答えて

10

あなたはセルを知っているし、コントロールが住んでいることを行た場合は、LINQを使用することができますそれをつかむための声明。

ここでは、私はプライベート変数を追加し、グリッドに入れデ・コントロールを思い出すことができ、行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

ニース - 再びLINQはループの必要性を排除します。 –

1

Grid.GetRowメソッドとGrid.GetColumnメソッドを使用して行と列の値をチェックし、値が一致したときに対象のコンテンツを置き換えることができます。ここでは、WPFでテストサンプルは、ですが、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> 

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

を使用すると、多数または行/ colsのを持っている場合は、残りの子供を反復を避けるためにbreak文を追加したい場合がありますことを追加しておく必要がありますあなたの目標を達成した後。 –

0

、3欄にある最初のコントロールを取得しますLINQ文のです:

private Control controlCentral = null; 

次に、この変数にグリッドに追加するコントロールを配置します。グリッドのコントロールを削除するには、Removeを使用します。

次のコードは、行0のコントロールを置き換えるコラム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; 
    } 
関連する問題