【问题标题】:How do objects NOT implementing INPC notify changes?未实现 INPC 的对象如何通知更改?
【发布时间】:2015-01-12 23:55:04
【问题描述】:

给知道答案的人一个简单的问题:

我有一个基本的 Person 类定义如下:

public class Person
{
   public Person(string name, string surname)
   {
        this.Name = name;
        this.Surname = surname;
   }

   public string Name { get; set; }

   public string Surname { get; set; }
}

一个非常简单的 ViewModel

public partial class SimpleViewModel : Screen
{
    public SimpleViewModel()
    {
        this.Persons = new ObservableCollection<Person>(this.GetPersons());
    }

    private ObservableCollection<Person> persons;
    public ObservableCollection<Person> Persons
    {
        get { return this.persons; }
        set
        {
            if (this.persons == value) return;

            this.persons = value;
            this.NotifyOfPropertyChange(() => this.Persons);
        }
    }


    private Person selectedPerson;
    public Person SelectedPerson
    {
        get { return this.selectedPerson; }
        set
        {
            if (this.selectedPerson == value) return;

            this.selectedPerson = value;
            this.NotifyOfPropertyChange(() => this.SelectedPerson);
        }
    }

    IEnumerable<Person> GetPersons()
    {
        return
            new Person[]
            {
                new Person("Casey", "Stoner"),
                new Person("Giacomo", "Agostini"),
                new Person("Troy", "Bayliss"),
                new Person("Valentino", "Rossi"),
                new Person("Mick", "Doohan"),
                new Person("Kevin", "Schwantz")
            };
    }
}

还有一个非常简单的视图

<Window x:Class="Test.SimpleView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SimpleView"
        Width="300"
        Height="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="8"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <ListView x:Name="lsv"
                  ItemsSource="{Binding Persons}"
                  SelectedItem="{Binding SelectedPerson}" Grid.RowSpan="3">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding FirstName}" />
                    <GridViewColumn DisplayMemberBinding="{Binding LastName}" />
                </GridView>
            </ListView.View>
        </ListView>

        <StackPanel Grid.Row="2"
                    Grid.Column="1"
                    VerticalAlignment="Center">
            <TextBox Text="{Binding SelectedPerson.FirstName, UpdateSourceTrigger=PropertyChanged}" />
            <TextBox Text="{Binding SelectedPerson.LastName, UpdateSourceTrigger=PropertyChanged}" />
        </StackPanel>

    </Grid>
</Window>

如果我在文本框中编辑 FirstNameLastName,列表视图会更新。

如果Person 没有实现INotifyPropertyChanged,那怎么可能?

谢谢

附: ViewModelCaliburn.Micro 继承 Screen

【问题讨论】:

  • FirstName 和 LastName 来自哪里? Person 只有 Name 和 Surname 属性。

标签: c# .net mvvm observablecollection inotifypropertychanged


【解决方案1】:

我不确定,但我认为........

  1. 您的 Persons 属性实现了 INotifyPropertyChanged 接口,因此,您的 Person 类中的所有对象或属性也间接实现了 INotifyPropertyChanged。

  2. 在这种情况下我可能错了。 ObservableCollection 还在内部实现了 INotifyPropertyChanged。因此,无需在 Persons 属性中实现 INotifyPropertyChanged。

【讨论】:

    【解决方案2】:

    这是使用PropertyDescriptor 传播更改通知as described here

    我不会依赖这种绑定。

    • 它比实施 INPC 更慢更重(最好 POCO 对象的实践建议)。
    • 它仅适用于更改 通过 Binding 语法启动。如果你以编程方式 更改名称的值,列表不会响应。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 2013-05-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 2011-12-04
    相关资源
    最近更新 更多