使用一个ObservableCollection [英] Using an ObservableCollection
问题描述
嗨
我一直在搜索当前日期以解决我遇到的一个有约束力的问题,发现的每个解决方案都出现了问题,无法正常工作.我能想到的是,我正在尝试执行的操作不受支持并且无法完成(但我对此表示怀疑.)这是场景.....
我有一类叫做SupportCall
在该类内,有一个SupportAction对象的ObservableCollection.
基于查看支持呼叫的用户的安全权限,他们可以看到所有SupportAction或仅公开的.我通过具有如下所示的两个属性来公开列表.
Hi
I have been searching fo days now to resolve a binding issue i am having and every solution i found has come up short and not worked. All i can think of is that what i am trying to do is not supported and cannot be done (But I Doubt it.) Here is the scenario.....
I have a class called SupportCall
Within the class there is an ObservableCollection of a SupportAction object.
Based on the security rights of the user viewing the support call they can see All SupportAction''s or just public ones. i expose the lists By having two properties as shown below.
Public ReadOnly Property Actions() As IList(Of SupportAction)
Get
Try
Dim found As IQueryable(Of SupportAction) = Nothing
found = From a As SupportAction In _actions.AsQueryable _
Order By a.CreateDate Descending
Return found.ToObservableCollection
Catch ex As Exception
Throw ex
End Try
End Get
End Property
还有....
And....
Public ReadOnly Property ActionsGeneral() As IList(Of SupportAction)
Get
Try
Dim found As IQueryable(Of SupportAction) = Nothing
found = From a As SupportAction In _actions.AsQueryable _
Where a.IsInternal = False Order By a.CreateDate Descending
Return found.ToObservableCollection
Catch ex As Exception
Throw ex
End Try
End Get
End Property
我使用扩展功能将IQueryable转换为ObservableCollection.该功能在下面.....
i use an extention fucntion to convert the IQueryable to an ObservableCollection. The function is below.....
<Extension()> _
Public Function ToObservableCollection(Of T)(ByVal MyCollection As IEnumerable(Of T)) As ObservableCollection(Of T)
Try
Dim c As New ObservableCollection(Of T)(MyCollection)
Return c
Catch ex As Exception
Throw New NotSupportedException(ex.InnerException.Message)
End Try
End Function
在我的前端WPF应用程序中,我使用其中一个属性来填充数据网格,就像这样....
In my front end WPF application i use one of the properties to populate a datagrid like so....
dgActions.ItemsSource = _CurrentCall.Actions
我有一个按钮,然后添加一个新的动作.这是通过SupportCall对象中的方法完成的.该方法如下....
i have a button that then adds a new action. This is done via a method within the SupportCall Object. The method is below....
Public Sub AddAction(ByVal UserID As Integer, ByVal Description As String, ByVal HasAtt As Boolean, ByVal AttFileName As String, ByVal Type As Integer, ByVal Internal As Boolean)
Try
Dim sa As New SupportAction(_callid, UserID, Description, HasAtt, AttFileName, Type, Internal, _datasource, _DSusername, _DSPassword)
_actions.Add(sa)
NotifyPropertyChanged("Actions")
NotifyPropertyChanged("ActionsGeneral")
Catch ex As Exception
Throw ex
End Try
End Sub
我的问题是,当我触发AddAction子对象时,我希望WPF数据网格能够反映更改,因为其源已设置为ObservableCollection. Unfortunatley没有任何反应.我必须关闭表单,然后在网格中重新加载新动作的视图.我需要能够在代码中动态绑定Datagrid.ItemSource而不是XAML来应用用户安全功能.谁能告诉我我在这里到底在做什么错,因为我无法使它正常工作....请帮助某人……
my problem is that when i fire the AddAction sub i expect the WPF datagrid to reflect the change as its source has been set to be an ObservableCollection. Unfortunatley nothing happens. I have to close the form and reload the view the new action in the grid. I need to be able to bind the Datagrid.ItemSource dynamically in code rather than XAML to apply user security features. Can anyone tell me what the hell i am doing wrong here because i just cant get it to work.... Please Help Someone......
推荐答案
您调试好了,调用NotifyPropertyChanged("Actions")
时会发生什么.是否真的调用了Actions()属性.可以肯定的是,在Actions属性中放置一个断点,以防万一调试器由于某种原因而无法跳转到该属性.
如果调用了该方法,则可以尝试将新的布尔值添加到该类,然后使用该布尔值首先返回null作为集合,然后返回实际的集合,该怎么办.就像这样:
Have you debugged, what happens when you callNotifyPropertyChanged("Actions")
. Is the Actions() property really called. To be sure, place a breakpoint inside the Actions property, just in case the debugger cannot jump to the property for some reason.
If it''s called, you could try what happens if you add a new boolean to the class and using that you first return a null as the collection and then the actual collection. So something like:
Public ReadOnly Property Actions() As IList(Of SupportAction)
Get
If _returnEmpty Then
Return null
End If
Try
Dim found As IQueryable(Of SupportAction) = Nothing
found = From a As SupportAction In _actions.AsQueryable _
Order By a.CreateDate Descending
Return found.ToObservableCollection
Catch ex As Exception
Throw ex
End Try
End Get
End Property
还有
And
Public Sub AddAction(ByVal UserID As Integer, ByVal Description As String, ByVal HasAtt As Boolean, ByVal AttFileName As String, ByVal Type As Integer, ByVal Internal As Boolean)
Try
Dim sa As New SupportAction(_callid, UserID, Description, HasAtt, AttFileName, Type, Internal, _datasource, _DSusername, _DSPassword)
_actions.Add(sa)
_returnEmpty = true
NotifyPropertyChanged("Actions")
_returnEmpty = false
NotifyPropertyChanged("Actions")
NotifyPropertyChanged("ActionsGeneral")
Catch ex As Exception
Throw ex
End Try
End Sub
绑定不喜欢戏剧性的变化.查看您的代码而不进行测试,似乎您每次调用扩展功能时都在传递一个新的集合.这导致绑定断开.
有两(2)种可能的解决方案:
1.调用扩展程序修复刹车后,在代码后面发出绑定命令;
2.将具体的ObservableCollection传递给扩展,并在填充之前使用Clear方法清空集合.
第二个选项是我要使用的选项.
Bindings don''t like dramatic changes. Looking at your code, without testing it, it appears that you''re passing a new collection every time the Extension funtion is called. This is causing the binding to become broken.
There are two (2) possible solutions:
1. Issue a bind command in code-behind after calling the extension to fix the brake;
2. Pass a concrete ObservableCollection to the extension and use the Clear method to empty the collection before filling.
The second option is the one that I would use.
伙计们
只是想让您知道您解决了我的问题.我已经调试了代码,并且新事件已正确触发.我接受了Graeme的建议,并尝试了第二种方法,并尝试清除并填充原始的ObservableCollection.我试图记住,我只是在使用链接来排序和过滤,而不必使用委托.因此,我将代码更改为以下代码,并且一切正常,谢谢.我已经提供了代码解决方案,以防其他人遇到相同的问题.
Hi Guys
just wanted you to know that you fixed my issue. I had already debugged the code and new that the events were firing correctly. I took Graeme''s advice and tried option two and tried to clear and populate the original ObservableCollection. I tried to remember that i was only using link to sort and filter without having to use delegates. So i changed my code to below and all works perfectly so thanks. I have included the code solution incase anyone else come against the same issue.
Public ReadOnly Property Actions() As IList(Of SupportAction)
Get
Try
SortActions()
Return _actions
Catch ex As Exception
Throw ex
End Try
End Get
End Property
Private Sub SortActions()
Try
Dim ReturnItems As IList(Of SupportAction)
Dim found As IQueryable(Of SupportAction) = Nothing
found = From a As SupportAction In _actions.AsQueryable Order By a.CreateDate Descending
ReturnItems = found.ToList
_actions.Clear()
For Each sa As SupportAction In ReturnItems.ToObservableCollection
_actions.Add(sa)
Next
Catch ex As Exception
Throw ex
End Try
End Sub
这篇关于使用一个ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!