【问题标题】:Silverlight: Delete content of a specific cell in a gridSilverlight:删除网格中特定单元格的内容
【发布时间】:2009-04-14 08:56:27
【问题描述】:

我有一个网格,我需要动态替换驻留在其中一个单元格中的控件。我不知道精确定位网格单元格的语法,我在哪里输入行号和列号,以便删除其中的任何内容。

【问题讨论】:

    标签: silverlight grid


    【解决方案1】:

    如果您知道控件所在的单元格和行,则可以使用 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 再次消除了循环的需要。
    【解决方案2】:

    您可以使用 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 语句以避免在你达到目标后迭代剩余的孩子。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多