我有的ObservableCollection的2個實例使用2個observableCollections用相同引用對象失去值dependencyProperties的 - WPF
Public Property Reports As New ObservableCollection(Of Report)
Public Property AvailableReports As New ObservableCollection(Of Report)
第一列表是在初始化時填充,並且第二被填充對象從第一清單中這方面的一定條件下。
For Each rep As Report In Reports
If rep.Width = _customWidth
AvailableReports.Add(rep)
End If
Next
這樣做時,Report中的dependencyProperty'IsSelected'丟失了它的值;所以如果Reports列表中的一個對象'x'具有'IsSelected = True',那麼在將其添加到AvailableReports列表後,'x.IsSelected'將返回False。
下面是報告類
Public Class Report
Inherits DependencyObject
Implements IComparable(Of Report)
Public Property Layout As Byte()
Public Property reportId As Integer
Public Property Name As String
Public Property Width As Double
Public Shared ReadOnly IsSelectedProperty As DependencyProperty = DependencyProperty.Register("IsSelected", GetType(Boolean), GetType(Report))
Public Property IsSelected As Boolean
Get
Return GetValue(IsSelectedProperty)
End Get
Set(value As Boolean)
SetValue(IsSelectedProperty, value)
End Set
End Property
Public Function CompareTo(other As Report) As Integer Implements IComparable(Of Report).CompareTo
Return Me.Width.CompareTo(other.Width)
End Function
末級
可能您在IsSelected上的綁定成爲本地值。 – Wouter
也許因爲您正在分配而不檢查If rep.Width = _customWidth –
您可以顯示類ReportTemplteLayout。它應該不是這樣的:AvailableReports.Add(rep)? – Rekshino