Click here to Skip to main content
15,886,812 members
Articles / Desktop Programming / WPF

Navigation in MVVM applications (Onyx)

Rate me:
Please Sign up or sign in to vote.
4.33/5 (7 votes)
13 Nov 2009CPOL4 min read 69.9K   2.2K   28  
Navigation in MVVM applications. Combined with navigation through DataTemplates, navigation through NavigationService provides a richer toolkit for WPF developers. I think it would be useful to utilize NavigationService, since the development of WPF applications then becomes similar to web developme
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace NavigationDemo
{
    public class NavPanel : Panel, INotifyPropertyChanged
    {
        #region Fields
        private FrameworkElement _visualParent;
        private Size _lastVisualParentSize;
        private Size _max;
        private double _childrenWidth;
        #endregion //Fields

        #region .ctor
        public NavPanel() { }
        #endregion //.ctor

        #region Overrides and Handlers
        protected override Geometry GetLayoutClip(Size layoutSlotSize)
        {
            return null;
        }
        protected override Size MeasureOverride(Size avaluableSize)
        {
            Size max = new Size();

            UIElement _child;

            for(int i=0; i < this.InternalChildren.Count; i++)
            {
                _child = this.InternalChildren[i];
                _child.Measure(avaluableSize);

                max.Width = Math.Max(max.Width,_child.DesiredSize.Width);
                max.Height = Math.Max(max.Height, _child.DesiredSize.Height);
            }

            _max = max;

            if (double.IsInfinity(avaluableSize.Width) || double.IsInfinity(avaluableSize.Height))
            {
                return new Size(MaxWidth * this.InternalChildren.Count, MaxHeight);
            }
            else
            {
                return new Size(avaluableSize.Width * this.InternalChildren.Count, avaluableSize.Height);
            }
        }
        protected override Size ArrangeOverride(Size finalSize)
        {
            UIElement _child;

            if (_visualParent != null) 
            {
                _lastVisualParentSize = _visualParent.RenderSize;
            }
            else
            {
                _lastVisualParentSize = finalSize;
            }

            for(int i=0; i< this.InternalChildren.Count; i++)
            {
                _child =  this.InternalChildren[i];
                Rect rect = new Rect(new Point(_max.Width * i, 0), _max);
                _child.Arrange(rect);
            }
            ChildrenWidth = _max.Width * InternalChildren.Count;

            return new Size(_lastVisualParentSize.Width, _lastVisualParentSize.Height);
        }
        protected override void OnVisualParentChanged(DependencyObject oldParent)
        {
            base.OnVisualParentChanged(oldParent);

            _visualParent = this.VisualParent as FrameworkElement;
        }
        #endregion //Overrides and Handlers

        #region Properties
        public double ChildrenWidth
        {
            get { return _childrenWidth; }
            set
            {
                if (_childrenWidth != value)
                {
                    _childrenWidth = value;
                    OnPropertyChanged("ChildrenWidth");
                }
            }
        }
        #endregion //Properties

        #region INotifyPropertyChanged
        private void OnPropertyChanged(string name)
        {
            try
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion //INotifyPropertyChanged
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
I have Master degree in Particle Physics. During my last several years I work as software developer.

Primary Interests
- c#, c++, php, java.
- scientific programming

Comments and Discussions