简体   繁体   English

C#WPF WrapPanel问题

[英]C# WPF WrapPanel Issue

Kind of hard to explain, but I have a WrapPanel that includes data that is displayed based on an index i. 有点难以解释,但是我有一个WrapPanel,其中包含基于索引i显示的数据。 The index i changes depending on if the user selects something from a ComboBox. 索引i取决于用户是否从ComboBox中选择了某些内容。 The problem is is when the user chooses a new option from the ComboBox, the new wrap/data overlaps the previous wrap/data. 问题是,当用户从ComboBox中选择一个新选项时,新的换行/数据与以前的换行/数据重叠。 I want the first initial wrap to show, then when the SelectedIndex changes, the previous wrap should be hidden and the new wrap based on the new index to be shown. 我希望显示第一个初始换行,然后在SelectedIndex更改时,应隐藏前一个换行,并基于新索引显示新换行。 Here's some sample code: 这是一些示例代码:

private void fillColumns(int i, int colIndex, int rowIndex) //Called each time SelectedIndex is changed.
{
    System.Windows.Controls.WrapPanel wrap1 = new System.Windows.Controls.WrapPanel();
    wrap1.Orientation = System.Windows.Controls.Orientation.Vertical;
    wrap1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
    wrap1.Margin = new Thickness(2, 2, 2, 2);

    System.Windows.Controls.TextBlock courseTextBlock = new System.Windows.Controls.TextBlock();
    courseTextBlock.Inlines.Add(new Run("Course: ") { Foreground = Brushes.Purple, FontWeight = FontWeights.Bold });
    courseTextBlock.Inlines.Add(returnedTable.Tables[0].Rows[i]["course_prefix"].ToString() + " " + returnedTable.Tables[0].Rows[i]["course_num"].ToString());
    courseTextBlock.Margin = new Thickness(2, 2, 2, 2);
    wrap1.Children.Add(courseTextBlock);

    Grid.SetColumn(wrap1, colIndex);
    Grid.SetRow(wrap1, rowIndex);
    tabGrid1.Children.Add(wrap1)
    //Clear WrapPanel after user chooses new ComboBox option?
}

It's a little unclear how this code is called, and how the variables colIndex and rowIndex relate to the ComboBox, but assuming i is the variable that is changing it would seem that the problem is that every time the ComboBox selection is changed, the contents based on row i are added to the grid cell at position (colIndex, rowIndex) in addition to any content already present in that cell rather than replacing it. 尚不清楚该代码的调用方式以及变量colIndex和rowIndex与ComboBox的关系,但是假设i是要更改的变量,似乎问题在于每次ComboBox选择更改时,内容都基于在第i行上,除了该单元格中已经存在的任何内容(而不是替换它)之外,还将其添加到网格单元中的位置(colIndex,rowIndex)。

The easiest way of modifying your current code would be to clear the grid cell before adding wrap1 to the grid. 修改当前代码的最简单方法是在将wrap1添加到网格之前清除网格单元。 eg 例如

Replace 更换

tabGrid1.Children.Add(wrap1)

with

// Remove any existing content at this position in the grid
foreach (var existingContent in (from cell in tabGrid1.Children where Grid.GetRow(cell) == rowIndex && Grid.GetColumn(cell) == colIndex select cell).ToArray())
{
    tabGrid1.Children.Remove(existingContent);
}

// add the new content
tabGrid1.Children.Add(wrap1);

This isn't particularly elegant though, and dynamicaly setting or binding to the content of the TextBlock would be preferable to creating a new one each time. 不过,这并不是特别优雅,并且动态设置或绑定到TextBlock的内容比每次创建一个新的文本集要好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM