2017-04-03 24 views
0

我有的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 

末級

+0

可能您在IsSelected上的綁定成爲本地值。 – Wouter

+0

也許因爲您正在分配而不檢查If rep.Width = _customWidth –

+0

您可以顯示類ReportTemplteLayout。它應該不是這樣的:AvailableReports.Add(rep)? – Rekshino

回答

0

我無法重現您的問題的代碼。如果我創建了一個對象並將它們放到不同的ObservableCollections中,那麼這個對象也具有相同的值,也是依賴屬性,並且它不能是其他值,因爲這些集合共享同一個對象,它是引用類型。如果我將這些集合綁定到不同的UI控件並綁定IsSelected屬性,那麼在一個控件中更改屬性會導致在另一個控件中更改選擇。
修改您的代碼,無論您發佈的代碼是否與您的實際代碼相匹配。它應該工作。

+0

最有可能我有一個與用戶界面的問題,這是造成這種行爲,thnx – AbbasB

相關問題