列表框无法显示其所有项目内容
我正在开发一个 C#/WPF 程序,并创建了一个由两个文本框(和一个网格)组成的用户控件。
我的主窗口中有一个列表框。它的项目源设置为我提到的用户控件的集合。当我填充它并调试程序时,它包含 20 个项目,并且所有项目中都有我想要的文本。但是在加载主窗口(并且填充列表框)后,它会显示所有项目,但其中只有九个具有带有文本的文本框。其他的有文本框,没有任何文本。
这可能是什么原因?
更新:这是代码。
用户控件:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox x:Name="NumberColumn" x:FieldModifier="public" Text="{Binding LineNumber}"
Grid.Column="0" HorizontalAlignment="Right" />
<TextBox x:Name="TextColumn" x:FieldModifier="public" Text="{Binding Text}"
Grid.Column="1" HorizontalAlignment="Left" />
</Grid>
列表框的填充:
string line;
Collection<CustomLine> lines = new Collection<CustomLine>();
StreamReader reader = new StreamReader(@"2008.sln");
while ((line = reader.ReadLine()) != null)
{
CustomLine myLine = new CustomLine(line, lines.Count + 1);
lines.Add(myLine);
}
this.leftListBox.ItemsSource = lines;
列表框的XAML:
<ListBox x:Name="leftListBox" Grid.Column="0" Margin="10"
MouseDown="leftListBox_MouseDown" SelectionChanged="leftListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<UserControl>
<local:CustomLine />
</UserControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果可能使用另一段代码,我也可能稍后添加它。
I'm working on a C#/WPF program and created a user control consisting of two textboxes (and a grid).
There is a listbox in my main window. Its items source is set to a collection of my mentionend user controls. When I fill it and debug the program it holds 20 items and all items have text in it as I wanted it. But after the main window is loaded (and the listbox filled) it shows all items, but only nine of them have text boxes with text. The other ones have text boxes without any text.
What may be the reason for this?
Update: here is the code.
The user control:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox x:Name="NumberColumn" x:FieldModifier="public" Text="{Binding LineNumber}"
Grid.Column="0" HorizontalAlignment="Right" />
<TextBox x:Name="TextColumn" x:FieldModifier="public" Text="{Binding Text}"
Grid.Column="1" HorizontalAlignment="Left" />
</Grid>
The filling of the listbox:
string line;
Collection<CustomLine> lines = new Collection<CustomLine>();
StreamReader reader = new StreamReader(@"2008.sln");
while ((line = reader.ReadLine()) != null)
{
CustomLine myLine = new CustomLine(line, lines.Count + 1);
lines.Add(myLine);
}
this.leftListBox.ItemsSource = lines;
The XAML of the listbox:
<ListBox x:Name="leftListBox" Grid.Column="0" Margin="10"
MouseDown="leftListBox_MouseDown" SelectionChanged="leftListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<UserControl>
<local:CustomLine />
</UserControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
If there another piece of code might be using, I may add it later, too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要将
CustomLine
控件包装在UserControl
标记中?我最好的猜测是
UserControl
告诉ListBox
这些项目的大小不正确,因此ListBox
仅加载它认为的项目是可见的。这是因为ListBox
的默认行为是使用 UI 虚拟化,因此它只渲染可见的控件,滚动 ListBox 将重用现有控件并替换它们后面的 DataContext,而不是创建新的控件。尝试删除
ItemTemplate
中的UserControl
Why did you wrap your
CustomLine
control in aUserControl
tag?My best guess is that the
UserControl
is telling theListBox
that the items are an incorrect size, so theListBox
is only loading the items it thinks are visible. This is because the default behavior of aListBox
is to use UI Virtualization, so it only renders the controls that are visible, and scrolling the ListBox will re-use existing controls and replace the DataContext behind them instead of creating new controls.Try removing the
UserControl
in yourItemTemplate