【问题标题】:WPF Delete Row from gridWPF 从网格中删除行
【发布时间】:2013-12-31 16:20:46
【问题描述】:

我对 WPF 很陌生,正在使用 WPF 构建图表应用程序。我正在动态添加新行,并且效果很好。删除行时出现问题。这是我添加行的代码

RowDefinition newRow = new RowDefinition();
newRow.Name = "ADX";
newRow.Height = new GridLength(1, GridUnitType.Star);
this.chartForm.sciChartControl.ContentGrid.RowDefinitions.Add(newRow);
Grid.SetRow(scs, this.chartForm.sciChartControl.ContentGrid.RowDefinitions.Count - 1);
this.techIndicatorToRowDefinitionMap["ADX"] = newRow;

删除行的代码是

this.chartForm.sciChartControl.ContentGrid.RowDefinitions.Remove(this.techIndicatorToRowDefinitionMap["ADX"]);

当我删除 rows 时,似乎随机行被删除了。你能告诉我是否有更简单的方法来跟踪行并删除它们,或者这段代码中是否存在错误。

谢谢。

【问题讨论】:

    标签: c# wpf grid


    【解决方案1】:

    您好,我认为您的代码正确删除了 RowDefinition,但我认为错误的是您还需要删除该行中 Grid 的子项,例如

    this.chartForm.sciChartControl.ContentGrid.Children.Remove(scs);
    this.chartForm.sciChartControl.ContentGrid.RowDefinitions.Remove(this.techIndicatorToRowDefinitionMap["ADX"]);
    

    如果您不删除子项,则 RowDefinition 将被删除,但子项将被转移到另一行。我希望这会给您一个想法。

    【讨论】:

    • 虽然这个答案是正确的,但应该注意的是,您还需要将该行以下的所有孩子也向上移动。要么,要么选择删除所有子项和 RowDefinitions 并完全重建它。