Hank
Hank

Reputation: 2616

How to call method on custom control via ViewModel

I am using the MVVM pattern in my project. It uwas using code-behind.

The issue that I'm having is: Included in my project I have a page transition control that I downloaded from Simple WPF Page transitions.


It worked great in code-behind, the xaml as follows:

<Grid ShowGridLines="False">
    <pageTransitions:PageTransition Name="pageTransitionControl" Margin="0" TransitionType="GrowAndFade" />
</Grid>

with this in the window tag:

xmlns:pageTransitions="clr-namespace:WpfPageTransitions;assembly=WpfPageTransitions" 


In the code-behind I just ran:

mast.Page mp = new mast.Page();
pageTransitionControl.ShowPage(mp);

When I do the below code-behind, it unloads the current page (mp) and loads the new one (dp)

dist.Page dp = new dist.Page();
pageTransitionControl.ShowPage(dp);

Above "mp" and "dp" are new instances of a UserControl (page). pageTransitionControl is the name of the transition control in the xaml.

Now I would like to get it to run via the ViewModel, without communicating with the view as it is doing above, how can I go about this?

Upvotes: 2

Views: 1074

Answers (1)

odyss-jii
odyss-jii

Reputation: 2699

Ideally, the PageTransition control would provide a way for you to set the current page via binding. Assuming that it does not provide a way of doing that, then there are a number of ways of achieving this.

Here are three suggestions, in order of "niceness" (in my opinion).

  1. You can create a new page transition control which either is a wrapper for PageTransition or inherits it. And then add a DependecyProperty for the current page to that class which you can bind to, catch the dependecy property change event and call ShowPage.

  2. Write a class inhering FrameworkElement or DependencyObject, depending on usage, which can bind to a page and to the PageTransition control. This class is then responsible for calling ShowPage on the bound PageTransition control when the current page changes.

  3. Bind the PageTransition control to a property on your model and have code in the model access the control via that property.

Example:

public class MyPageTransition : ContentControl
{
    public static readonly DependencyProperty CurrentPageProperty = 
        DependencyProperty.Register(
            "CurrentPage", 
            typeof(object), 
            typeof(MyPageTransition), 
            new PropertyMetadata(DependencyPropertyChanged));

    public ContentControl()
    {
        this.Content = this.pageTransition;
    }

    public object CurrentPage
    {
        get { return GetValue(CurrentPageProperty); }
        set { SetValue(CurrentPageProperty, value); }
    }

    protected static void DependencyPropertyChanged(
        DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        if (e.Property == CurrentPageProperty)
        {
            this.pageTransition.ShowPage(CurrentPage);
        }
    }

    private PageTransition pageTransition = new PageTransition();
}

Upvotes: 3

Related Questions