【问题标题】:How to reflect propertychange for an entity to ViewModal in WPF如何将实体的属性更改反映到 WPF 中的 ViewModel
【发布时间】:2010-09-30 05:23:59
【问题描述】:

我有一个继承自 INOTIFYPROPERTYCHANGED 的实体,该实体绑定到我的 UI 元素,如 TextBOX。现在,当任何属性发生更改时,我的绑定都会更新并且一切正常。我想要的是在我的视图中捕获这个 propertychanged 事件。我的 viewmodal 也继承自 INOTIFYPROPERTYCHANGED。


实体

public class DIPREDIPForView:INotifyPropertyChanged
{
    #region PrivateFields
    private string fieldDelimiter, textQualifier;

    #endregion
    public DIPREDIPForView()
    {

    }
    public string FieldDelimiter
    {
        get
        {
            return fieldDelimiter;
        }
        set
        {
            fieldDelimiter = value;
            OnPropertyChanged("FieldDelimiter");
            //NotifyViewModalEvent();
        }
    }
    public string TextQualifier {
        get
        {
            return textQualifier;
        }
        set
        {
            textQualifier = value;
            OnPropertyChanged("TextQualifier");
            //NotifyViewModalEvent();
        }
    }
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;

    public virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
    #endregion

视图模式

public class DIPREDIPViewModel : BaseViewModel
{

    public DIPREDIPForView Configuration
    {
        get
        {
            return configuration;
        }
        set
        {
            configuration = value;               
        }
    }

}

BaseViewModal

public class BaseViewModel : INotifyPropertyChanged, IDisposable
{
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;


    public virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
    #endregion


}

我想要的是每当“配置”发生变化时,应该通知 ViewModal,因为我必须基于此调用其他逻辑。

我尝试在 Configuration 的设置器中调用 OnPropertyChanged(),但它不会触发。每当我更新 UI 上的值时,此配置都会更新,但设置器中的断点不会被命中。我应该如何做到这一点。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    视图模型或视图模型基础都没有订阅 PropertyChanged 事件。因此,当 DIPREDIPForView 引发事件时,这些对象都不会收到事件通知。

    大概 DIPREDIPViewModel 需要订阅 DIPREDIPForView 上的 PropertyChanged,而事件订阅需要指向 BaseViewModel 上的 OnPropertyChanged 事件处理器。

    不确定这是否是您要查找的内容。

    这是一个示例...

    [Test]
    public void TestEventIsRaised()
    {
        TestEventSubscriber subscriber = new TestEventSubscriber();
        subscriber.SetFirstName("joe");
        Assert.IsTrue(subscriber.EventWasHandledInTestDouble);
    }
    
    
    public class TestEventSubscriber : EventSubscriber
    {
        public bool EventWasHandledInTestDouble;
        public TestEventSubscriber()
        {
    
        }
    
        protected override void OnMyEvent(object sender, EventArgs args)
        {
            EventWasHandledInTestDouble = true;
        }
    }
    
    public class EventSubscriber
    {
        private EventRaiser eventRaiser = new EventRaiser();
    
        public EventSubscriber()
        {
            eventRaiser.MyEvent += (sender, args) => {
                Console.Write("Event was handled via anonymous delegate");
            };
    
            eventRaiser.MyEvent = OnMyEvent;
        }
    
        public void SetFirstName(string firstName)
        {
            eventRaiser.FirstName = "bob";
        }
    
        protected virtual void OnMyEvent(object sender, EventArgs args)
        {
            Console.WriteLine("Event was handled via virtual method...");
        }
    }
    
    
    public class EventRaiser
    {
        public EventHandler MyEvent;
    
        public EventRaiser()
        {
    
        }
    
        private string firstName;
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
                EventHandler handler = this.MyEvent;
                if (handler != null)
                    MyEvent(this, EventArgs.Empty);
            }
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      ViewModel 是模型的视图。
      我会考虑以下几点:

      1. 将 DIPREDIPForView 类型的成员添加到类 DIPREDIPViewModel
      2. 使 DIPREDIPForView 成为不实现 INotifyPropertyChanged 的​​模型
      3. 公开属性:FieldDelimiter、TextQualifier 和 Configuration 均来自 DIPREDIPViewModel
      4. 将视图的 DataContext (XAML) 设置为 ViewModel DIPREDIPViewModel 以进行绑定。

      这将您需要的一切都保存在一个类中,并使更新多个属性更简单。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-28
        • 1970-01-01
        • 2019-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多