Created
December 2, 2015 10:57
-
-
Save tiesmaster/cf9e808e744e8f0cc2f4 to your computer and use it in GitHub Desktop.
Add support in PRISM for setting header of TabControl
This file contains 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
namespace Coolblue.Wpf.Toolkit.Regions | |
{ | |
public interface IRegionHostedInTabControlAware | |
{ | |
string Header { get; } | |
} | |
} |
This file contains 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.Collections; | |
using System.Collections.Specialized; | |
using System.Windows; | |
using System.Windows.Controls; | |
using Coolblue.Wpf.Toolkit.Regions.RegionBehaviours.Helpers; | |
using Prism.Regions; | |
using Prism.Regions.Behaviors; | |
namespace Coolblue.Wpf.Toolkit.Regions.RegionBehaviours | |
{ | |
public class RegionHostedInTabControlAwareBehavior : RegionBehavior, IHostAwareRegionBehavior | |
{ | |
public const string BEHAVIOR_KEY = "RegionHostedInTabControlAwareBehavior"; | |
private readonly ViewModelFromViewExtractor _viewModelFromViewExtractor; | |
private TabControl _hostTabControl; | |
public RegionHostedInTabControlAwareBehavior() | |
{ | |
_viewModelFromViewExtractor = new ViewModelFromViewExtractor(); | |
} | |
public DependencyObject HostControl { get; set; } | |
protected override void OnAttach() | |
{ | |
_hostTabControl = HostControl as TabControl; | |
if(_hostTabControl != null) | |
{ | |
if(_hostTabControl.IsLoaded) | |
ApplyTabControlAwareToTabItems(Region.Views); | |
else | |
_hostTabControl.Loaded += (s, e) => ApplyTabControlAwareToTabItems(Region.Views); | |
Region.Views.CollectionChanged += RegionViewsOnCollectionChanged; | |
} | |
} | |
private void RegionViewsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) | |
{ | |
if(e.Action == NotifyCollectionChangedAction.Add) | |
ApplyTabControlAwareToTabItems(e.NewItems); | |
} | |
private void ApplyTabControlAwareToTabItems(IEnumerable newRegionViews) | |
{ | |
foreach(var newRegionView in newRegionViews) | |
{ | |
var tabControlAware = newRegionView as IRegionHostedInTabControlAware; | |
if(tabControlAware != null) | |
ApplyTabControlAwareToTabItem(newRegionView, tabControlAware); | |
else | |
{ | |
tabControlAware = | |
_viewModelFromViewExtractor.TryExtract(newRegionView) as IRegionHostedInTabControlAware; | |
if(tabControlAware != null) | |
ApplyTabControlAwareToTabItem(newRegionView, tabControlAware); | |
} | |
} | |
} | |
private void ApplyTabControlAwareToTabItem(object newRegionView, | |
IRegionHostedInTabControlAware tabControlAware) | |
{ | |
if(_hostTabControl.IsLoaded) | |
{ | |
var tabItemHostingRegion = GetTabItem(newRegionView); | |
tabItemHostingRegion.Header = tabControlAware.Header; | |
} | |
} | |
private TabItem GetTabItem(object tabControlAwareRegion) | |
{ | |
return (TabItem)_hostTabControl.ItemContainerGenerator.ContainerFromItem(tabControlAwareRegion); | |
} | |
} | |
} |
What does ViewModelFromViewExtractor
refer to?
ViewModelFromViewExtractor
:
using System.Windows;
internal class ViewModelFromViewExtractor
{
public object TryExtract(object view)
{
var result = default(object);
var newRegionViewWithDataContext = view as FrameworkElement;
if(newRegionViewWithDataContext != null)
result = newRegionViewWithDataContext.DataContext;
return result;
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use it properly? Is there a simple Demo?Thanks