WPF Delete Row from grid

2019-07-11 00:18发布

I am pretty new to WPF and am building an Charting application using WPF. I am adding new rows dynamically and it works perfectly. I am seeing a problem when removing rows. This is my code for adding rows

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;

and the code to remove the Row is

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

When I remove the rows , it seems like random rows are removed. Can you tell me if there is an easier way to keep track of rows and delete them or if there is a bug in this code .

Thanks.

标签: c# wpf grid
1条回答
何必那么认真
2楼-- · 2019-07-11 00:50

Hi I think your code is Removeing the RowDefinition correctly but what I think wrong is you also need to remove the children of Grid in that Row like

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

If you wont remove the child the RowDefinition will be removed but child will be shifted to another row .I hope this will give you an idea.

查看更多
登录 后发表回答