WPF: call Command in MVVM after view is rendered?

I am designing a WPF app and using MVVM pattern. Let's say I have one view called View1 , and it's DataContext is set to ViewModel1 in it's contractor. The VM has one Command called RefreshCommand whose job is to calculate the data to be displayed on the view. Now I'd like to call this RefreshCommand right after my view is shown, but I don't know how to do that.

I tried to call it in the code-behind where I handle the Loaded event for the usercontrol, something like this:

Private Sub AfterLoad(sender As Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    RefreshButton.Command.Execute(sender)
End Sub

However, that's when I noticed that at this moment the RefreshButton.Command is still set as Nothing . Note that in the UI interaction the invoke of this button/command is fine. So when should I call the command to perform such operation? Thanks!


That looks like it should work. Show your XAML and viewmodel?

Normally I would just have my VM get its data immediately and show the data in the View via bindings. Try executing the Refresh command in your VM constructor

链接地址: http://www.djcxy.com/p/9602.html

上一篇: WPF使用MVVM模式浏览视图

下一篇: WPF:在渲染视图后调用MVVM命令?