Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window Loaded and WPF

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, though. I am attempting to use the following code:

In my xaml:

<interactivity:Interaction.Behaviors>
    <behaviors:WindowLoadedBehavior LoadedCommand="{Binding WindowLoadedCommand}" />
</interactivity:Interaction.Behaviors>

In my View Model:

private DelegateCommand _WindowLoadedCommand;

public DelegateCommand WindowLoadedCommand
{
    get
    {
        return _WindowLoadedCommand;
    }
    private set
    {
        _WindowLoadedCommand = value;
    }
}

public ShellViewModel()
{
    WindowLoadedCommand = new DelegateCommand(WindowLoadedAction);
}

protected void WindowLoadedAction()
{
    ...
}

My attached behavior:

public class WindowLoadedBehavior : Behavior<FrameworkElement>
{
    [SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Dependency Property.  Allow public.")]
    public static DependencyProperty LoadedCommandProperty = DependencyProperty.Register("LoadedCommand", typeof(ICommand), typeof(WindowLoadedBehavior), new PropertyMetadata(null));

    public ICommand LoadedCommand
    {
        get { return (ICommand)GetValue(LoadedCommandProperty); }
        set { SetValue(LoadedCommandProperty, value); }
    }

    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.Loaded += AssociatedObject_Loaded;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.Loaded -= AssociatedObject_Loaded;

        base.OnDetaching();
    }

    private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
    {
        if (LoadedCommand != null)
            LoadedCommand.Execute(null);
    }
}

The OnAttached, AssociatedObject_Loaded and LoadedCommand get are all firing, but the LoadedCommand set is not firing and, obviously, the WindowLoadedCommand isn't firing. Any clue what I can do to get this working?

like image 502
Dirk Dastardly Avatar asked Sep 25 '12 19:09

Dirk Dastardly


People also ask

What is loaded in WPF?

According to normal behavior of WPF window, Loaded event occurs when the element is laid out, rendered, and ready for interaction.It is the last event raised in an element initialization sequence.

What is difference between WPF page and window?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.

Is Microsoft still supporting WPF?

Net Core 3.0. This demonstrates that Microsoft still has faith in WPF as a user interface framework, and the company is still willing to invest in it by updating and integrating it with its new offerings.” “Microsoft released WPF, WinForms, and WinUI on the same day it released.NET Core 3.0 Preview 1.

What is difference between user control and window in WPF?

A window is managed by the OS and is placed on the desktop. A UserControl is managed by wpf and is placed in a Window or in another UserControl. Applcations could be created by have a single Window and displaying lots of UserControls in that Window.


1 Answers

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);
}
like image 196
Harlow Burgess Avatar answered Sep 24 '22 00:09

Harlow Burgess