How can i RaisePropertyChanged on property change?

后端 未结 2 1555
小鲜肉
小鲜肉 2020-12-18 04:40

Here I added a model to my viewmodel...

public dal.UserAccount User  {
  get
  {
    return _user;
  }
  set
  {
    _user = value;
    RaisePropertyChanged(         


        
2条回答
  •  孤城傲影
    2020-12-18 05:03

    You can invoke a property changed event from another class. Not particularly useful if you have all the sources. For closed source it might be. Though I consider it experimental and not production ready.

    See this console copy paste example:

    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApp1
    {
        public class Program
        {
            static void Main(string[] args)
            {
                var a = new A();
                a.PropertyChanged += A_PropertyChanged;
                var excpl = new Excpl();
                excpl.Victim = a;
                excpl.Ghost.Do();
            }
    
            private static void A_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                Console.WriteLine("Event triggered");
            }
        }
    
        [StructLayout(LayoutKind.Explicit)]
        public struct Excpl
        {
            [FieldOffset(0)]
            public A Victim;
            [FieldOffset(0)]
            public C Ghost;
        }
    
        public class A : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
        public class C : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void Do()
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(""));
            }
        }
    }
    

提交回复
热议问题