如果我创建一个带有自动增长列的简单 ListView,它只会自动增长添加的第一个“批次”项目。为什么会发生这种情况,有什么办法可以解决吗?
我已经尝试过使用 ObservableCollection(显示示例),并手动将项目添加到 LstItems.Items,行为相同。第一次单击 BtnAdd 时,它会按预期自动增长,但随后不会。
XAML:
<ListView Margin="12,12,12,41" Name="LstItems">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Row 1" DisplayMemberBinding="{Binding Path=Col1}" />
<GridViewColumn Width="Auto" Header="Row 2" DisplayMemberBinding="{Binding Path=Col2}" />
</GridView>
</ListView.View>
</ListView>
代码:
public partial class Window1 : Window
{
private readonly ObservableCollection<MyData> _data = new ObservableCollection<MyData>();
private const int NumRepeats = 2;
private const int EnumerationIncrement = 3;
private int _enumerationCount = 3;
public Window1()
{
InitializeComponent();
LstItems.ItemsSource = _data;
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i <= NumRepeats; i++)
{
string data = String.Join(", ", Enumerable.Repeat("Test Data", _enumerationCount));
_data.Add(new MyData { Col1 = "Test", Col2 = data });
_enumerationCount += EnumerationIncrement;
}
}
}