【问题标题】:WPF INotifyPropertyChanged not updating labelWPF INotifyPropertyChanged 未更新标签
【发布时间】: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


【解决方案1】:

如果您有 Visual Studio 15,请使用 NameOf 运算符而不是字符串文字,如下所示:

NameOf(PropMoney);

如果您稍后重命名您的属性,它仍然可以工作,而字符串文字则不会。或者修改您的 OnPropertyChange 以使用 CallerMemberName

OnPropertyChange ([System.Runtime.CompilerServices.CallerMemberName] string memberName = "")
{

}

将填写属性名称,但这仅适用于当前属性的设置器。

另外,为整个窗口设置 DataContext (Setting DataContext in XAML in WPF)。 DataContext={StaticResource ViewModel} 并且不要在绑定中使用路径,只需 {Binding PropertyName}

【讨论】:

  • 我必须在哪里插入/替换 NameOf(PropMoney)?
  • 在你的 OnPropertyChange 方法调用中,而不是 ""PropMoney""。
【解决方案2】:

您的OnPropertyChanged("mycurrentMoney") 语句不会引发您的财产的财产变更,因为它被称为PropMoney。

你必须在你的设置器中设置OnPropertyChanged("PropMoney")。

【讨论】:

    【解决方案3】:

    您的代码有 2 个问题

    首先你为支持字段引发 PropertyChanged 事件,并且应该为属性名称引发它

    OnPropertyChanged("PropMoney")
    

    您更改的第二个属性属于View 的不同实例,然后设置为DataContext。所以在 XAML 中删除 DataContext 更改,只保留属性绑定

    <Window ...>
        <Grid Margin="0,0,2,-4">
            <!-- .... -->
            <Label ... Content="{Binding Path=PropMoney}">
    

    然后在 MainWindow 的代码集 DataContext 中添加到您创建和修改的实例

    Class MainWindow
    
    Private view As New View
    
        Sub New()
            InitializeComponent()
            DataContext = view
        End Sub
    
        Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
            view.SelfClicked()
        End Sub
    
    End Class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      相关资源
      最近更新 更多