INotifyPropertyChanged不工作,WPF中的数据绑定
我正在列表框中添加一些数据,并使用Inotifypropertychanged通知, 它的代码很简单,但不起作用,点击按钮后列表框中的数据不会改变INotifyPropertyChanged不工作,WPF中的数据绑定,wpf,vb.net,data-binding,Wpf,Vb.net,Data Binding,我正在列表框中添加一些数据,并使用Inotifypropertychanged通知, 它的代码很简单,但不起作用,点击按钮后列表框中的数据不会改变 <Grid> <StackPanel Orientation="Horizontal" > <ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" > </ListBox&
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
我的xaml
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
ss尝试更新绑定,如下所示
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
ItemsSource="{Binding listoffiles, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
您的绑定可能在初始过程中正常工作,但它不会使用属性保持更新状态。尝试如下更新绑定
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
ItemsSource="{Binding listoffiles, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
您的绑定可能在初始过程中正常工作,但它不会随属性保持更新。您需要将
列表更改为可观察集合ObservableCollection
有一个名为NotifyCollectionChanged
的事件。调用更新集合的方法(如Add()
、Remove()
)时引发此事件
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
因此,您需要对以下代码进行更改,以使其正常工作
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
将列表文件
更改为公共属性
在main窗口的构造函数中设置datacontext
在main窗口的构造函数中将listoffiles
设置为等于viewmodel.listoffiles
下面是您在上面发布的代码的工作版本
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
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
您需要将列表
更改为可观察集合
ObservableCollection
有一个名为NotifyCollectionChanged
的事件。调用更新集合的方法(如Add()
、Remove()
)时引发此事件
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
因此,您需要对以下代码进行更改,以使其正常工作
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
将列表文件
更改为公共属性
在main窗口的构造函数中设置datacontext
在main窗口的构造函数中将listoffiles
设置为等于viewmodel.listoffiles
下面是您在上面发布的代码的工作版本
<Grid>
<StackPanel Orientation="Horizontal" >
<ListBox x:Name="listbox1" Width=" 250" ItemsSource="{Binding listoffiles}" >
</ListBox>
<Button x:Name="btntest" Width=" 50" Height=" 20" VerticalAlignment="Top" Margin=" 10" Content=" Test it"></Button>
<TextBox x:Name="textbox1" Height=" 50" Width=" 100" Margin="0,135" Text="{Binding text}"/>
</StackPanel>
</Grid> </Window>
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
但它仍然不起作用。看起来很直截了当,但是很令人沮丧。。它不工作!!!!但在初始化中工作时,它会显示默认数据,直到不工作为止。看起来很直截了当,但是很令人沮丧。。它不工作!!!!但在初始化中工作时,它会显示默认数据。如果尝试执行此操作。。。但当我单击按钮时,列表框中的数据仍然不变。您在上述代码中使用过Inotifypropertychanged吗?你能发布你的xaml吗?还是不行!!xaml是相同的。但是,我确实为按钮使用了不同的名称。我将名称更改回您使用的名称,但忘记更改单击事件。我编辑了我的文章,所以如果你在代码中复制它应该可以工作。只需确保单击事件的名称与您的按钮对应,我没有使用INotifyPropertyChanged,因为我只关心集合。对于不是集合的属性,只能使用InotifyPropertyChanged`。如果属性是一个集合,您需要实现INotifyCollectionChanged
,或者您可以只使用已经实现它的observateCollection
。尝试了这样做。。。但当我单击按钮时,列表框中的数据仍然不变。您在上述代码中使用过Inotifypropertychanged吗?你能发布你的xaml吗?还是不行!!xaml是相同的。但是,我确实为按钮使用了不同的名称。我将名称更改回您使用的名称,但忘记更改单击事件。我编辑了我的文章,所以如果你在代码中复制它应该可以工作。只需确保单击事件的名称与您的按钮对应,我没有使用INotifyPropertyChanged,因为我只关心集合。对于不是集合的属性,只能使用InotifyPropertyChanged`。如果属性是一个集合,您需要实现INotifyCollectionChanged
,或者您可以只使用已经实现它的observedcollection
。检查我对我的帖子所做的编辑检查我对我的帖子所做的编辑