INotifyPropertyChanged无效,WPF中的数据绑定

时间:2016-01-29 19:53:19

标签: wpf vb.net data-binding

我在列表框中添加一些数据并使用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

2 个答案:

答案 0 :(得分:2)

您需要将List更改为ObservableCollectionObservableCollection有一个名为NotifyCollectionChanged的活动。调用更新集合的Add()Remove()等方法时会引发此事件。

因此,您需要对此进行以下代码更改

  1. listoffiles更改为公共财产
  2. datacontext
  3. 的构造函数中设置MainWindow
  4. listoffiles
  5. 的构造函数中将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}"

您的绑定可能在初始传递时正常工作,但它不会保持对该属性的更新。