在 MVVM 中将事件从项目路由到集合

发布于 2024-12-17 13:38:36 字数 859 浏览 1 评论 0原文

我怎样才能做这样的事情?

public class person 
{
  public ICommand Add_as_Friend { get; private set; }

  public event EventHandler C1_Friend_Add;

  //....

  Add_as_Friend = new Command(Handle_Add_FR, HandleCan_Add_FR);
  void Handle_Add_FR(object parameter)
  {
    Friend_Add(this, new EventArgs());
  }
}

public class person_Collection : ObservableCollection<person>
{
  //.....
  //???
}

public class MainViewModel : ViewModelBase
{
  public person_Collection person_List{ get; set; }
  public person_Collection person_List2{ get; set; }

  person_Collection.???.item.Friend_Add += new EventHandler(Add);

  void Add(object sender, EventArgs e)
  {
    myPerson = sender as person;
    person_List2.add(myPerson);
    //...
  }
} 

ICommand Add_as_Friend 是 ItemsControl 中的一个按钮。

我需要将我的事件发送到 MainViewModel 而不是人。

How can I do something like this?

public class person 
{
  public ICommand Add_as_Friend { get; private set; }

  public event EventHandler C1_Friend_Add;

  //....

  Add_as_Friend = new Command(Handle_Add_FR, HandleCan_Add_FR);
  void Handle_Add_FR(object parameter)
  {
    Friend_Add(this, new EventArgs());
  }
}

public class person_Collection : ObservableCollection<person>
{
  //.....
  //???
}

public class MainViewModel : ViewModelBase
{
  public person_Collection person_List{ get; set; }
  public person_Collection person_List2{ get; set; }

  person_Collection.???.item.Friend_Add += new EventHandler(Add);

  void Add(object sender, EventArgs e)
  {
    myPerson = sender as person;
    person_List2.add(myPerson);
    //...
  }
} 

The ICommand Add_as_Friend is a Button in an ItemsControl.

I need to send my Event to the MainViewModel rather than the person.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2024-12-24 13:38:36

您可以观察您的 ObservableCollection 并将 EventHandler 注册到所有类似的新项目(我没有完全使用您的类结构,您必须为您的项目修改它):

public class MainViewModel : ViewModelBase
{
    public ObservableCollection<Person> Persons { get; set; }

    public MainViewModel()
    {
        Persons = new ObservableCollection<Person>();
        Persons.CollectionChanged += PersonCollectionChanged;
    }

    private void PersonCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if(e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach(Person person in e.NewItems)
            {
                person.Friend_Add += new EventHandler(Add);
            }
        }
    }
}

编辑: 下面是一个实现您要使用的 PersonCollection 类的名称。现在您可以选择是否要使用其中一种实现。 (我更喜欢第一个)

public class Person
{
    public event EventHandler AddedFriend;
}

public class PersonCollection : ObservableCollection<Person>
{
    public event EventHandler AddedFriend;

    public PersonCollection() : base(new ObservableCollection<Person>())
    {
        base.CollectionChanged += PersonCollectionChanged;
    }

    private void PersonCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (Person person in e.NewItems)
            {
                person.AddedFriend += PersonAddedFriend;
            }
        }
    }

    private void PersonAddedFriend(object sender, EventArgs e)
    {
        var addedFriend = AddedFriend;
        if (addedFriend != null)
        {
            addedFriend(sender, e);
        }
    }
}

You could oberve your ObservableCollection and register the EventHandler to all new items like that (I'm not exactly using your classes structure, you will have to modify it for your project):

public class MainViewModel : ViewModelBase
{
    public ObservableCollection<Person> Persons { get; set; }

    public MainViewModel()
    {
        Persons = new ObservableCollection<Person>();
        Persons.CollectionChanged += PersonCollectionChanged;
    }

    private void PersonCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if(e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach(Person person in e.NewItems)
            {
                person.Friend_Add += new EventHandler(Add);
            }
        }
    }
}

Edit: underneath is an implementation of the PersonCollection class you want to use. Now you can choose if you want to use one of those implementations. (I would prefer the first)

public class Person
{
    public event EventHandler AddedFriend;
}

public class PersonCollection : ObservableCollection<Person>
{
    public event EventHandler AddedFriend;

    public PersonCollection() : base(new ObservableCollection<Person>())
    {
        base.CollectionChanged += PersonCollectionChanged;
    }

    private void PersonCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (Person person in e.NewItems)
            {
                person.AddedFriend += PersonAddedFriend;
            }
        }
    }

    private void PersonAddedFriend(object sender, EventArgs e)
    {
        var addedFriend = AddedFriend;
        if (addedFriend != null)
        {
            addedFriend(sender, e);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文