2

我在 View 的构造函数中将我的 View 的 DataContext 设置为我的 ViewModel 的一个实例,只是标准的东西。此后不久,一个UPDATE_RECENT_DOCUMENTS_LIST事件从我的 ViewModel 正确捕获的事件聚合器中触发。更改了属性并调用了 onPropertyChanged 方法,但由于 PropertyChanged 事件为空而失败。

我要做的下一件事是对 UI 执行一个操作,该操作引发CREATE_PROJECT事件,并且同一个 ViewModel 正在接收事件,但现在,PropertyChanged 事件不再为空,一切都按预期工作。

在设置 DataContext 之后,在它注册到 PropertyChanged 事件之前是否需要经过特定的时间量?是否有一个我可以等待的事件来确保 PropertyChanged 事件不为空?

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

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

工具栏视图.xaml.cs:

namespace ToolBarModule
{   

public partial class ToolBarView : UserControl
    {                           
        public ToolBarView(ToolBarViewModel toolBarViewModel)
        {
            InitializeComponent();             
            this.DataContext = 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;
        }
    }
}

}

4

2 回答 2

1

您还可以尝试在 UserControl 下方设置 Grid 或 Element 的 DataContext。对我来说,它奏效了。

示例(如果您使用 DependencyProperty,则不起作用):

代码背后:

public MyUserControl()
{
     InitializeComponent();
     this.DataContext = new { LabelText = "Hello World!" };
}

XAML

<UserControl x:Class="CoolProject.ViewModel.MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Label x:Name="myLabel" Content="{Binding LabelText}"/>

示例 2(我的工作代码):

代码背后:

public MyUserControl()
{
     InitializeComponent();
     this.myGrid.DataContext = new { LabelText = "Hello World!" };
}

XAML

<UserControl x:Class="CoolProject.ViewModel.MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">

<Grid x:Name="myGrid">
    <Label x:Name="myLabel" Content="{Binding LabelText}"/>
</Grid>

于 2020-05-11T11:37:41.787 回答
0

您必须命名您的UserControlinXAML并在绑定中使用它。类似于以下代码:

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

uc您的名称在哪里UserControl,并且还尝试在加载 UserControl 时设置 DataContext。

希望这有帮助。

于 2012-08-09T00:30:22.707 回答