Window Loaded and WPF

后端 未结 1 1566
猫巷女王i
猫巷女王i 2021-01-12 13:44

I have a WPF project in Windows 2012 in which I need to load some information in the Window Loaded event. I need to do this in the View Model rather than in the CodeBehind,

相关标签:
1条回答
  • 2021-01-12 14:22

    There are a few options. A couple of them listed here:

    how to call a window's Loaded event in WPF MVVM?

    However, in the off chance that you or anyone else cares that you are spending several hours to complete a task that should have taken 30 seconds, you might want to try this instead.

    public MainWindow()
    {
        InitializeComponent();
    
        this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }
    
    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        ShellViewModel.Instance.WindowLoadedCommand.Execute(null);
    }
    
    0 讨论(0)
提交回复
热议问题