Created
October 15, 2015 11:58
-
-
Save Snegovikufa/2419560c5121ca444a59 to your computer and use it in GitHub Desktop.
MultiPagePanel for WPF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Collections.Specialized; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Markup; | |
namespace Swag.UI.Views | |
{ | |
[DefaultProperty("PagesCollection")] | |
[ContentProperty("PagesCollection")] | |
public sealed class MultiPagePanel : ContentControl | |
{ | |
public static readonly DependencyProperty PagesCollectionProperty = | |
DependencyProperty.Register( | |
"PagesCollection", | |
typeof (ObservableCollection<FrameworkElement>), | |
typeof (MultiPagePanel), | |
new PropertyMetadata(null)); | |
public static readonly DependencyProperty LoadedPageProperty = | |
DependencyProperty.Register( | |
"LoadedPage", | |
typeof (object), | |
typeof (MultiPagePanel), | |
new PropertyMetadata(0)); | |
public MultiPagePanel() | |
{ | |
PagesCollection = new ObservableCollection<FrameworkElement>(); | |
Subscribe(); | |
} | |
public ObservableCollection<FrameworkElement> PagesCollection | |
{ | |
get { return (ObservableCollection<FrameworkElement>) GetValue(PagesCollectionProperty); } | |
set { SetValue(PagesCollectionProperty, value); } | |
} | |
public object LoadedPage | |
{ | |
get { return GetValue(LoadedPageProperty); } | |
set { SetValue(LoadedPageProperty, value); } | |
} | |
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs) | |
{ | |
Unsubscribe(); | |
} | |
private void Subscribe() | |
{ | |
Loaded += OnLoaded; | |
PagesCollection.CollectionChanged += OnPagesCollectionChanged; | |
DataContextChanged += MultiPagePanel_DataContextChanged; | |
Unloaded += OnUnloaded; | |
} | |
private void Unsubscribe() | |
{ | |
PagesCollection.CollectionChanged -= OnPagesCollectionChanged; | |
DataContextChanged -= MultiPagePanel_DataContextChanged; | |
} | |
private void OnLoaded(object sender, RoutedEventArgs e) | |
{ | |
if (LoadedPage == null) | |
{ | |
Content = null; | |
} | |
else if (LoadedPage is int) | |
{ | |
SwitchPage((int) LoadedPage); | |
} | |
else | |
{ | |
string page = LoadedPage as string; | |
if (page != null) | |
{ | |
SwitchPage(page); | |
} | |
else | |
{ | |
Content = null; | |
} | |
} | |
} | |
public void SwitchPage(int idx) | |
{ | |
if (idx < 0 || idx >= PagesCollection.Count) | |
{ | |
return; | |
} | |
Content = PagesCollection[idx]; | |
} | |
public void SwitchPage(string name) | |
{ | |
foreach (var item in PagesCollection) | |
{ | |
if (item.Name == name) | |
{ | |
Content = item; | |
LoadedPage = name; | |
return; | |
} | |
} | |
Content = null; | |
} | |
private void OnPagesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) | |
{ | |
if (e.NewItems != null) | |
{ | |
foreach (FrameworkElement item in e.NewItems) | |
{ | |
item.DataContext = DataContext; | |
} | |
} | |
} | |
private void MultiPagePanel_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) | |
{ | |
foreach (var page in PagesCollection) | |
{ | |
page.DataContext = DataContext; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment