Change WPF Window Layout Using MVVM

Ok, this is basically a criticism against the simplified solution you’re going to find on SO and their biased voting mechanism.

stackoverflow-logo-dumpster

For a similar thing, I used a transition control in XAML (you can see the custom control class at the bottom of my post)

 <local:GridExt  FrontView="{Binding SelFrontView}" x:Name="TransitionGrid"
...

<Controls:TransitioningContentControl x:Name="transitioning"      HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="120, 20, 20, 20"     Transition="Left">

</Controls:TransitioningContentControl>

and a dependency property in code behind

        DependencyPropertyDescriptor dp_fw = DependencyPropertyDescriptor.FromProperty(GridExt.MyEnumProperty, typeof(GridExt));
        dp_fw.AddValueChanged(TransitionGrid, async (object a, EventArgs b) =>
        {
            switch (GridExt.GetFrontView(TransitionGrid))
            {
                case FrontView.Home:
                    // do something
                    LbHome.Visibility = Visibility.Visible;
                    break;
                case FrontView.Check:
                    TogglePopupButton.IsChecked = false;
                   // or something else
                    transitioning.Content = check;
                    LbHome.Visibility = Visibility.Hidden;
                    break;
                default:
                    break;
            }
        });

so that I could guide the transition from the ViewModel with a simple command

#region Transition Command
    private DelegateCommand transitionCommand;
    public ICommand TransitionCommand
    {
        get
        {
            if (transitionCommand == null)
            {
                transitionCommand = new DelegateCommand((parameter) => TransitionLogic(), (parameter) => CanTransition());
            }
            return transitionCommand;
        }
    }

    public bool CanTransition()
    {
        return true;
    }
    public void TransitionLogic()
    {
        SelFrontView = FrontView.Check;
    }

    #endregion

Find below my custom control class

  public class GridExt : Grid
    {
        public static readonly System.Windows.DependencyProperty MyEnumProperty;


        static GridExt()
        {
            MyEnumProperty = System.Windows.DependencyProperty.RegisterAttached(
                "FrontView", typeof(FrontView), typeof(GridExt));
        }

        public static void SetFrontView(System.Windows.UIElement element, FrontView value)
        {
            element.SetValue(MyEnumProperty, value);
        }
        public static FrontView GetFrontView(System.Windows.UIElement element)
        {
            return (FrontView)element.GetValue(MyEnumProperty);
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s