我在列表框中添加一些数据并使用Inotifypropertychanged来通知, 在列表框中单击按钮数据后,它的代码非常简单但不起作用
我的xaml
> `Imports System.Collections.ObjectModel Imports System.ComponentModel` > > Public Class testdata > Implements INotifyPropertyChanged > > Public Property listoffiles As List(Of String) > Get > Return _listoffiles > End Get > Set(value As List(Of String)) > _listoffiles = value > onpropertchange("listoffiles") > End Set > End Property > > Public Property text As String > Get > Return _text > End Get > Set(value As String) > _text = value > onpropertchange("text") > End Set > End Property > > > Private _listoffiles As New List(Of String) > Private _text As String > > Public Sub New() > _listoffiles.Add("One") > _listoffiles.Add("One") > _listoffiles.Add("One") > > End Sub > > Protected Sub onpropertchange(name As String) > RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name)) > End Sub > > Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged End Class My view.vb > Class MainWindow > > > Private Property viewmodel As testdata = New testdata > Private Sub btntest_Click(sender As Object, e As RoutedEventArgs) Handles btntest.Click > viewmodel.listoffiles.Add("two") > viewmodel.listoffiles.Add("two") > viewmodel.listoffiles.Add("two") > viewmodel.listoffiles.Add("two") > viewmodel.listoffiles.Add("two") > > > Dim listoffiles As List(Of String) = viewmodel.listoffiles > End Sub End Cla
我的viewmodiel类
function ClearOld() {
var ssActive = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(getSheetByName('Done'))
var MyRange = ssActive.getRange("C:C");//selects C column
var WeekAgo = new Date(date.getTime()-7*(24*3600*1000));
for(i = 0; i < MyRange.length; i++) {
if(MyRange[i].getTime()<WeekAgo){
getActiveSheet().deleteRow(i);
}
}
}
SS
答案 0 :(得分:2)
您需要将List
更改为ObservableCollection
。 ObservableCollection
有一个名为NotifyCollectionChanged
的活动。调用更新集合的Add()
,Remove()
等方法时会引发此事件。
因此,您需要对此进行以下代码更改
listoffiles
更改为公共财产datacontext
MainWindow
listoffiles
viewmodel.listoffiles
设置为等于MainWindow
醇>
下面是您在上面发布的代码的工作版本
Class MainWindow
Public Sub New()
InitializeComponent()
DataContext = Me
listoffiles = viewmodel.listoffiles
End Sub
Private Property viewmodel As testdata = New testdata
Public Property listoffiles As New ObservableCollection(Of String)
Private Sub btntest_Click(sender As Object, e As RoutedEventArgs) Handles bnttest.Click
viewmodel.listoffiles.Add("two")
viewmodel.listoffiles.Add("two")
viewmodel.listoffiles.Add("two")
viewmodel.listoffiles.Add("two")
viewmodel.listoffiles.Add("two")
End Sub
End Class
Public Class testdata
Implements INotifyPropertyChanged
Public Property listoffiles As ObservableCollection(Of String)
Get
Return _listoffiles
End Get
Set(value As ObservableCollection(Of String))
_listoffiles = value
End Set
End Property
Public Property text As String
Get
Return _text
End Get
Set(value As String)
_text = value
End Set
End Property
Private _listoffiles As New ObservableCollection(Of String)
Private _text As String
Public Sub New()
_listoffiles.Add("One")
_listoffiles.Add("One")
_listoffiles.Add("One")
End Sub
Protected Sub onpropertchange(name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
End Class
答案 1 :(得分:0)
尝试更新您的绑定,如下所示......
ItemsSource="{Binding listoffiles, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
您的绑定可能在初始传递时正常工作,但它不会保持对该属性的更新。