using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Presentation.Regions;
using System.Collections.Specialized;
namespace CompositeTest1.Common.Utilities
{
public class SLWindowRegionAdapter : RegionAdapterBase<Grid>
{
public SLWindowRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
: base(regionBehaviorFactory)
{ }
protected override void Adapt(IRegion region, Grid regionTarget)
{
}
protected override IRegion CreateRegion()
{
return new SingleActiveRegion();
}
protected override void AttachBehaviors(IRegion region, Grid regionTarget)
{
base.AttachBehaviors(region, regionTarget);
WindowRegionBehavior behavior = new WindowRegionBehavior(regionTarget, region);
behavior.Attach();
}
}
public class WindowRegionBehavior
{
private readonly WeakReference _ownerWeakReference;
private readonly WeakReference _regionWeakReference;
public WindowRegionBehavior(Grid owner, IRegion region)
{
_ownerWeakReference = new WeakReference(owner);
_regionWeakReference = new WeakReference(region);
}
public void Attach()
{
IRegion region = _regionWeakReference.Target as IRegion;
if (region != null)
{
region.Views.CollectionChanged += new NotifyCollectionChangedEventHandler(Views_CollectionChanged);
region.ActiveViews.CollectionChanged += new NotifyCollectionChangedEventHandler(ActiveViews_CollectionChanged);
}
}
public void Detach()
{
IRegion region = _regionWeakReference.Target as IRegion;
if (region != null)
{
region.Views.CollectionChanged -= Views_CollectionChanged;
region.ActiveViews.CollectionChanged -= ActiveViews_CollectionChanged;
}
}
void window_LostFocus(object sender, RoutedEventArgs e)
{
IRegion region = _regionWeakReference.Target as IRegion;
System.Windows.Controls.Window window = sender as System.Windows.Controls.Window;
if (window != null && region != null)
if (region.Views.Contains(window.Content))
region.Deactivate(window.Content);
}
void window_GotFocus(object sender, RoutedEventArgs e)
{
IRegion region = _regionWeakReference.Target as IRegion;
System.Windows.Controls.Window window = sender as System.Windows.Controls.Window;
if (window != null && !region.ActiveViews.Contains(window.Content) && region.Views.Contains(window.Content))
region.Activate(window.Content);
}
private void window_Closed(object sender, EventArgs e)
{
System.Windows.Controls.Window window = sender as System.Windows.Controls.Window;
IRegion region = _regionWeakReference.Target as IRegion;
if (window != null && region != null)
if (region.Views.Contains(window.Content))
region.Remove(window.Content);
Grid owner = _ownerWeakReference.Target as Grid;
StackPanel spanel = owner.FindName("SLWindow") as StackPanel;
if (spanel != null)
{
Button btn = spanel.FindName(window.Name.ToString().TrimEnd('M')) as Button;
spanel.Children.Remove(btn);
}
}
private void ActiveViews_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Grid owner = _ownerWeakReference.Target as Grid;
if (owner == null)
{
Detach();
return;
}
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (object view in e.NewItems)
{
System.Windows.Controls.Window window = GetContainerWindow(owner, view);
if (window != null)
{
window.Focus();
}
}
}
}
private void Views_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Grid owner = _ownerWeakReference.Target as Grid;
if (owner == null)
{
Detach();
return;
}
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (object view in e.NewItems)
{
System.Windows.Controls.Window window = new System.Windows.Controls.Window();
window.GotFocus += new RoutedEventHandler(window_GotFocus);
window.LostFocus += new RoutedEventHandler(window_LostFocus);
window.Closed += new EventHandler(window_Closed);
window.Content = view;
StackPanel spanel = owner.FindName("SLWindow") as StackPanel;
if (spanel != null)
{
int index = spanel.Children.Count + 1;
Button btn = new Button() { Name = "Window" + index, Content = "窗口" + index };
window.Name = "Window" + index+"M";
window.TitleContent = "窗口" + index;
btn.Tag = window;
btn.Click += new RoutedEventHandler(btn_Click);
spanel.Children.Add(btn);
}
window.Container = owner;
window.Show(DialogMode.Default);
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (object view in e.OldItems)
{
System.Windows.Controls.Window window = GetContainerWindow(owner, view);
if (window != null)
window.Close();
}
}
}
void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
System.Windows.Controls.Window window = btn.Tag as System.Windows.Controls.Window;
if (window != null)
{
window.Visibility = window.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
}
}
private System.Windows.Controls.Window GetContainerWindow(Grid owner, object view)
{
foreach (UIElement ui in owner.Children)
{
System.Windows.Controls.Window window = ui as System.Windows.Controls.Window;
if (window != null && window.Content == view)
return window;
}
return null;
}
}
}