4

グリッドがあり、セルの 1 つにあるコントロールを動的に置き換える必要があります。グリッド セルを特定するための構文がわかりません。行番号と列番号をどこに入力すれば、そこにあるものをすべて削除できるのでしょうか。

4

3 に答える 3

10

コントロールが存在するセルと行がわかっている場合は、LINQステートメントを使用してそれを取得できます。

これは、列3、行4にある最初のコントロールを取得するLINQステートメントです。

var control = (from d in grid.Children
               where Grid.GetColumn(d as FrameworkElement) == 3 
                  && Grid.GetRow(d as FrameworkElement) == 4
               select d).FirstOrDefault();
于 2009-04-15T02:15:15.037 に答える
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);
            }
        }

    }
于 2009-04-14T12:11:59.090 に答える
0

プライベート変数を追加してグリッドに配置したdecontrolを思い出すことができます。

private Control controlCentral = null;

次に、グリッドに追加するコントロールをこの変数に割り当てます。これにより、[削除]を使用してグリッドのコントロールを削除できます。

次のコードは、行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;
    }
于 2010-08-16T23:14:49.993 に答える