This is a migrated thread and some comments may be shown as answers.

MVVM bind to content rendered event

1 Answer 135 Views
Window
This is a migrated thread and some comments may be shown as answers.
Anne
Top achievements
Rank 1
Anne asked on 28 Dec 2020, 07:16 PM

Hello. 
We have a need to execute some command when the window is fully loaded. In order to do that we want to be able to bind event ContentRendered to the command in view model.

What would be the best way to achieve this? As I understood there are three approaches:

- resolving view model from the condebehind and invoking the command

- interactivity library (additional dependancy)

- a combination of attached property and attached behavior

 

What is a recommended way to bind to the window event in MVVM style? Is there a code sample somewhere to take a look at?

 

Thank you.

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 30 Dec 2020, 03:15 PM

Hello Anne,

Indeed, the approaches you listed are the ones usually suggested. In general all of those approaches are suitable and I cannot tell that there is a recommended one. It all depends on the project's requirements and the developer preferences. I would go with the "interactivity library" one because the Telerik UI for WPF suite already provides an extensions for this (EventToCommandBehavior), and also because it is the most clean solution. 

<Window x:Class="WpfApp8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">  
    <telerik:EventToCommandBehavior.EventBindings>
        <telerik:EventBinding EventName="ContentRendered" Command="{Binding MyCommand}" />
    </telerik:EventToCommandBehavior.EventBindings>
    <Grid>	
    </Grid>
</Window>

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Window
Asked by
Anne
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or