Silverlight:删除网格中特定单元格的内容

4

我有一个网格,需要动态替换一个控件,该控件位于其中一个单元格中。我不知道如何在语法上定位网格单元格,也不知道在哪里放置行号和列号,以便删除其中的任何内容。

3个回答

10

如果您知道控件所在的单元格和行,可以使用LINQ语句来获取它。

下面是一个LINQ语句,它将获取位于第3列第4行的第一个控件。

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

很好 - LINQ 再次消除了循环的需要。 - Gordon Mackie JoanMiro

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);
            }
        }

    }

应该补充说明,如果您有大量的行/列,您可能需要添加一个break语句来避免在达到目标后继续迭代剩余的子元素。 - Gordon Mackie JoanMiro

0

你可以记住我在网格中添加私有变量的控件:

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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接