本文介绍了设置DataContext后,PropertyChanged事件为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将View的构造方法中View的DataContext设置为ViewModel的实例,只是标准的东西.此后不久,从我的ViewModel正确捕获的Event Aggregator中触发了一个 UPDATE_RECENT_DOCUMENTS_LIST 事件.更改了属性并调用了onPropertyChanged方法,但是由于PropertyChanged事件为null,因此它失败了.

I am setting the DataContext for my View in the View's Constructor to an instance of my ViewModel, just standard stuff. Shortly thereafter, an UPDATE_RECENT_DOCUMENTS_LIST Event fires from the Event Aggregator which my ViewModel catches correctly. A property is changed and the onPropertyChanged method is called, but it fails as the PropertyChanged event is null.

我要做的第二件事是对UI的操作,该操作引发一个 CREATE_PROJECT 事件,并且相同的ViewModel正在接收事件,但现在,PropertyChanged事件不再为null,并且一切正常

The very next thing I do is an action to the UI which raises a CREATE_PROJECT Event and the same ViewModel is receiving events, except now, the PropertyChanged event is no longer null and everything works as expected.

设置DataContext之后,在注册到PropertyChanged事件之前是否要经过特定的时间?我是否可以等待确保PropertyChanged事件不为null的事件?

Is there a specific amount of time that has to pass after setting the DataContext before it registers to the PropertyChanged Event? Is there an event I can wait for that ensures the PropertyChanged event is not null?

此外,在集成Prism并使用非常方便的EventAggregator之后,我没有使用标准.NET事件遇到此问题.

Also, I did not run into this problem using standard .NET events, just after integrating Prism and using the very convenient EventAggregator.

我将代码显示在View和ViewModel的后面,为了简洁起见,省略了View XAML.

I am showing my code behind of the View and the ViewModel, omitting the View XAML for brevity.

ToolBarView.xaml.cs:

ToolBarView.xaml.cs:

namespace ToolBarModule
{

public partial class ToolBarView : UserControl
    {
        public ToolBarView(ToolBarViewModel toolBarViewModel)
        {
            InitializeComponent();
            this.DataContext = toolBarViewModel;
        }
    }
}

ToolBarViewModel.cs

ToolBarViewModel.cs

namespace ToolBarModule
{

public class ToolBarViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private ToolBarCommands baseCommands;
    private IEventAggregator eventAggregator;

    private KickStartEvent kickStartEvent;
    private SubscriptionToken subscriptionToken;

    private ObservableCollection<IDocumentReference> recentDocuments = new ObservableCollection<IDocumentReference>();

    private ActionCommand newTest;
    private ActionCommand openTest;
    private ActionCommand saveTest;
    private ActionCommand exitApplication;

    public ToolBarViewModel(){}

    public ToolBarViewModel(IEventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
        baseCommands = new ToolBarCommands(eventAggregator);
        kickStartEvent = eventAggregator.GetEvent<KickStartEvent>();
        subscriptionToken = kickStartEvent.Subscribe(kickStartEventHandler, ThreadOption.UIThread, true, toolBarEventHandlerFilter);
    }

    public ICommand NewTest
    {
        get
        {
            if (newTest == null)
            {
                newTest = new ActionCommand(baseCommands.NewTestAction);
            }
            return newTest;
        }
    }

    public ICommand OpenTest
    {
        get
        {
            if (openTest == null)
            {
                openTest = new ActionCommand(baseCommands.OpenTestAction);
            }
            return openTest;
        }
    }

    public ICommand SaveTest
    {
        get
        {
            if (saveTest == null)
            {
                saveTest = new ActionCommand(baseCommands.SaveTestAction);
            }
            return saveTest;
        }
    }


    public ICommand ExitApplication
    {
        get
        {
            if (exitApplication == null)
            {
                exitApplication = new ActionCommand(baseCommands.ExitApplicationAction);
            }
            return exitApplication;
        }
    }

    public ObservableCollection<IDocumentReference> RecentDocuments
    {
        get
        {
            return recentDocuments;
        }

        set
        {
            recentDocuments = value;
            onPropertyChanged("RecentDocuments");
        }
    }

    private void onPropertyChanged(string propertyChanged)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(propertyChanged));
        }

    }

    private void kickStartEventHandler(KickStartEventsArgs e)
    {
        switch (e.EventType)
        {
            case KickStartEventsArgs.KickStartEventType.CREATE_PROJECT:
                onPropertyChanged("RecentDocuments");
            break;


            case KickStartEventsArgs.KickStartEventType.UPDATE_RECENT_DOCUMENTS_LIST:
            RecentDocuments.Clear();

            foreach (IDocumentReference recentDocs in e.KickStartTestList)
            {
                RecentDocuments.Add(recentDocs);
            }
            onPropertyChanged("RecentDocuments");
            break;
        }
    }
}

}

推荐答案

您必须在XAML中命名UserControl并将其用于绑定.类似于以下代码:

You have to name your UserControl in XAML and use it in binding. Something like following code:

<UserControl x:Name="uc" >
.
.
.
<TextBox Text="{Binding UserName, Mode=TwoWay, ElementName=uc}"/>

其中uc是您的UserControl的名称,并且还尝试在加载UserControl时设置DataContext.

Where uc is a name of your UserControl, and Also try to set DataContext when UserControl loaded.

希望获得帮助.

这篇关于设置DataContext后,PropertyChanged事件为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 08:45