c# - PropertyChanged 在 ViewModelBase 中始终为 null

标签 c# wpf xaml mvvm binding

我做了一个简单的例子来更好地理解 MVVM 模式。 这是示例解决方案的链接,因为很难解释整个问题: http://www.2shared.com/file/jOOAnacd/MVVMTestMyCopy.html

Employee 模型(具有 Age 属性)和 EmployeeViewModel,其中包含 Employee 对象并更改其以下代码中的 Age 属性:

public int Age
{
    get { return _employee.Age; }
    set
    {
        if (value == _employee.Age)
            return;
        _employee.Age = value;
        NotifyPropertyChanged("Age");
    }
}

EmployeeViewModel 继承自 ViewModelBase 类,带有标准的 INotifyPropertyCHanged 代码:

if (PropertyChanged != null)
{
    PropertyChanged(this, new PropertyChangedEventArgs(p));
}

我正在尝试使用 ICommand 更改员工的年龄:

public void Increase()
{
    this.SelectedEmployee.Age++;
    NotifyPropertyChanged("Age");
}

属性已更改,但绑定(bind)的 TextBLock 未更改其值。 我检查发现 NotifyPropertyChanged 被调用,但是 PropertyChangednull。 我还确保我的应用程序中只有一个 PeopleViewModel。 那么,为什么 PropertyChangednull

编辑: 以下是 ViewModelBase 的完整代码:

public class ViewModelBase
{

    public String DisplayName { get; set; }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string p)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }


    #endregion

}

PeopleViewModel 包含 ObservalbleCollection 和 EmployeeViewModels 并设置为 DataContext。 属性的值已更改,但在不重新加载对象的情况下不会显示更改。

这是显示绑定(bind)的 PeopleViewer.xaml:

<UserControl x:Class="MVVMTestMyCopy.View.PeopleViewer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:vm="clr-namespace:MVVMTestMyCopy.ViewModel"
             mc:Ignorable="d" 
             d:DesignHeight="316" d:DesignWidth="410">
    <UserControl.Resources>
        <vm:PeopleViewModel x:Key="viewModel"/>
    </UserControl.Resources>
    <Grid DataContext="{Binding Source={StaticResource viewModel}}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <ListBox  ItemsSource="{Binding People}"
                  Grid.Column="0"
                  Margin="5,5,4,5"
                  SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Margin="2"
                                   Text="{Binding FirstName}" />
                        <TextBlock Margin="2"
                                   Text="{Binding LastName}" />
                        <TextBlock Margin="0 2"
                                   Text="[" />
                        <TextBlock Margin="2"
                                   Text="{Binding Age}" />
                        <TextBlock Margin="0 2"
                                   Text="]" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="0.75*" />
                <RowDefinition Height="0.25*" />
            </Grid.RowDefinitions>
            <Grid x:Name="EmployeeDetails"
                  Grid.Row="0"
                  DataContext="{Binding SelectedEmployee}"
                  Margin="5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding FirstName}"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Center"
                           Grid.Column="0"/>
                <TextBlock Text="{Binding LastName}"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Center"
                           Grid.Row="1" />
                <TextBlock Text="{Binding Age}"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Center"
                           Grid.Row="2" />

            </Grid>
            <StackPanel Orientation="Vertical"
                        HorizontalAlignment="Center"
                        Grid.Row="1">
                <Button x:Name="button"
                        Content="-"
                        Width="32"
                        Height="32"
                        Command="{Binding DecreaseCommand}">
                </Button>
                <Button x:Name="button1"
                        Content="+"
                        Width="32"
                        Height="32"
                        Command="{Binding IncreaseCommand}">
                </Button>
            </StackPanel>
        </Grid>
    </Grid>
</UserControl>

最佳答案

在您的项目中,您实际上并没有在 View 模型上实现 INotifyPropertyChanged。你有:

public class ViewModelBase

但这应该是:

public class ViewModelBase : INotifyPropertyChanged

因为您没有实现 INotifyPropertyChange,WPF 绑定(bind)系统将无法为您的 PropertyChanged 事件添加处理程序。

关于c# - PropertyChanged 在 ViewModelBase 中始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6829180/

相关文章:

xaml - 使用 XAML/C# 为 Windows 应用商店应用程序捕获并保存照片

c# - 元组的替代方法(setter)

Silverlight 3 - 将枚举从 DomainServiceContext 绑定(bind)到组合框

C# 命名空间结构迫使我使用别名

C# WPF XAML GridViewColumn 中的图标问题

c# - 为什么我的 WPF 绑定(bind)实现不能双向工作?

c# - DependencyProperty typeof GridLength 未将值 "Auto"传递给 CustomControl 中的 ChildElement

c# - 解析 C 源文件并获取函数列表

c# - c# 容器中的 push_back 操作

c# - 某些东西超越了我的约束