[英]WPF binded Listbox bug when display 2 items
I have a WPF listbox in my window. 我的窗口中有一个WPF列表框。 In the Load event of the window, i create a List(of Object) and I added some items.
在窗口的Load事件中,我创建了一个List(对象),并添加了一些项目。 At application starts or debug, I can see items.
在应用程序启动或调试时,我可以看到项目。 If I add 1 item on the list, i correctly see 1 only item.
如果我在列表中添加1个项目,则我只能正确看到1个项目。 If I add 3 or more items, i correctly see 3 or more items.
如果我添加3个或更多项目,我正确地看到3个或更多项目。 If I add 2 only items, i see 1 only item.
如果我仅添加2个项目,则只能看到1个项目。 Why?
为什么?
Here is my WPF code 这是我的WPF代码
<Window x:Class="Cacatua.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Cacatua;assembly=" >
<Grid>
<ListBox Name="lbSearch" ItemsSource="{Binding}" />
</Grid>
</Window>
And here is my code-behind (same assembly, in Cacatua namespace): 这是我的后台代码(相同的程序集,在Cacatua名称空间中):
Private myLstSearch As List(Of Object)
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
myLstSearch = New List(Of Object)
lbSearch.ItemsSource = myLstSearch
Dim myMedia1 as Media1
myMedia1 = New Media1("IdMedia1-A")
myLstSearch.Add(myMedia1)
myMedia1 = New Media1("IdMedia1-B")
myLstSearch.Add(myMedia1)
End Sub
where Media1 is a simple class that contains a string 其中Media1是包含字符串的简单类
Public Class Media1
Private myIdTitolo As String
Public ReadOnly Property IDTitolo As String
Get
Return (myIdTitolo)
End Get
End Property
Public Sub New(str As String)
myIdTitolo = str
End Sub
End Class
With this code, I would see a list with this output (there is no datatemplate): Cacatua.Media1 Cacatua.Media1 使用此代码,我将看到带有此输出的列表(没有数据模板):Cacatua.Media1 Cacatua.Media1
but I see only Cacatua.Media1 但我只看到Cacatua.Media1
I think it's a bug. 我认为这是一个错误。 But am I the first with this problem?
但是我是第一个遇到这个问题的人吗?
You've got the right idea, but the problem is your ItemsSource
doesn't know when to update since you're not using an ObservableCollection
. 您有一个正确的想法,但是问题是您的
ItemsSource
不知道何时更新,因为您没有使用ObservableCollection
。 Also there is a timing issue between rendering and loading the window, and I think this has to do with the fact you aren't properly binding your items source. 在渲染和加载窗口之间还存在时间问题,我认为这与您没有正确绑定项目源的事实有关。
For starters, try changing the type of myLstSearch
to ObservableCollection(Of Media1)
. 对于初学者,请尝试将
myLstSearch
的类型myLstSearch
为ObservableCollection(Of Media1)
。
Also, a better way to do this would be to databind it from the XAML directly, so your code-behind would be something like: 另外,更好的方法是直接从XAML对其进行数据绑定,因此您的代码隐藏内容将类似于:
Public property MyListSearch As ObservableCollection(Of Media1)
Then your XAML would look like: 然后,您的XAML如下所示:
<Window x:Class="Cacatua.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Cacatua;assembly=">
<Grid>
<ListBox Name="lbSearch" ItemsSource="{Binding Path=MyListSearch}" />
</Grid>
</Window>
That way, you can simply initialize MyListSearch
in your window constructor, and then add elements to it whenever, while your view will automatically update. 这样,您只需在窗口构造函数中初始化
MyListSearch
,然后随时向其添加元素,而视图将自动更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.