【发布时间】:2016-01-09 12:47:42
【问题描述】:
我目前正在学习 WPF 的一些基础知识,并且我一直在寻找错误大约 2 天。希望大家能帮忙。
我正在尝试通过使用 INotifyPropertyChanged 和 XAML 中的绑定来更新我的 UI(在本例中为标签的内容)。问题是:它只取第一个值并将其放入内容中。此外,除了触发事件 (OnPropertyChanged) 之外,什么也没有发生。
这就是我在 XAML 中的内容:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1" x:Class="MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:View x:Key="ViewModel"/>
</Window.Resources>
<Grid Margin="0,0,2,-4" DataContext="{Binding Source={StaticResource ViewModel}}">
....
<Label x:Name="lbl_money" Grid.ColumnSpan="2" Content="{Binding Path=PropMoney}" HorizontalAlignment="Left" Margin="403,42,0,0" VerticalAlignment="Top">
这是我的课堂视图的必要部分:
Public Class View
Inherits ViewModelBase
Private rest1 As New Restaurant
Private mainPlayer As New Player
Private mycurrentMoney As Double = 3
Private currentClickIncrease = mainPlayer.PropClickIncrease
Public Property PropMoney() As Double
Get
Return mycurrentMoney
End Get
Set(value As Double)
mycurrentMoney = value
OnPropertyChanged("mycurrentMoney")
End Set
End Property
Sub SelfClicked()
PropMoney() += 1
End Sub
最后但并非最不重要的 MainWindow 类,我在其中实例化我的视图:
Class MainWindow
Private view As New View
Sub New()
InitializeComponent()
End Sub
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
view.SelfClicked()
End Sub
End Class
所以我的 mycurrentMoney 每次点击都会增加,并且会触发事件,但标签不会更新。
提前谢谢你!
【问题讨论】:
-
改用
OnPropertyChanged("PropMoney") -
我做了但不幸的是没有效果
标签: wpf vb.net xaml inotifypropertychanged