Window_Load event in MVVM

爱⌒轻易说出口 提交于 2019-12-06 00:56:03

问题


I need to write some functions to be executed during the window_load() in WPF-MVVM. Every button will have their own command to be executed. Whereas is there any command available for window_load() event in MVVM Model ?


回答1:


You will have to use interactions to do that i.e to invoke command on event.

<Window
    xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
>
    <intr:Interaction.Triggers>
        <intr:EventTrigger EventName="Loaded">
            <intr:InvokeCommandAction Command="{Binding WindowLoaded}"/>
        </intr:EventTrigger>
    </intr:Interaction.Triggers>
    <!-- the rest of your XAML here -->
</Window>

Window.Interactivity namespace has EventTrigger and InvokeCommandAction.

Don't forget that the WindowLoaded is a property.

public ICommand WindowLoaded { get; set; }

You later have to create new RelayCommand/RoutedUICommand to actually receive the callback.

Thanks



来源:https://stackoverflow.com/questions/18593720/window-load-event-in-mvvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!