- mtvt_2010_5_1__12_15.zip
- Libraries
- DanielVaughan
- Prism
- Silverlight
- Microsoft.Practices.Composite.dll
- Microsoft.Practices.Composite.pdb
- Microsoft.Practices.Composite.Presentation.dll
- Microsoft.Practices.Composite.Presentation.pdb
- Microsoft.Practices.ServiceLocation.dll
- System.Windows.Controls.dll
- Mtvt
- DanielVaughan.Mtvt.sln
- DanielVaughan.Mtvt.suo
- DanielVaughan.Mtvt.vsmdi
- DanielVaughan.MtvtExample.Tests
- DanielVaughan.MtvtExample.Web
- DanielVaughan.MtvtExample
- Local.testsettings
- TraceAndTestImpact.testsettings
- Prism
- CAL
- CAL.vsmdi
- CompositeApplicationLibrary.4.5.resharper.user
- CompositeApplicationLibrary.sln
- CompositeApplicationLibrary_Desktop.4.5.resharper.user
- CompositeApplicationLibrary_Desktop.sln
- Desktop
- Composite.Presentation
- Composite
- LocalTestRun.testrunconfig
- Silverlight
- Composite.Presentation
- Bin
- Commands
- Composite.Presentation.Silverlight.csproj
- Composite.Presentation.Silverlight.csproj.user
- Events
- GlobalSuppressions.cs
- Migrated rules for Composite.Presentation.Silverlight.ruleset
- obj
- Debug
- build.force
- Composite.Presentation.Silverlight.csproj.FileListAbsolute.txt
- Composite.Presentation.Silverlight.csproj.GenerateResource.Cache
- DesignTimeResolveAssemblyReferencesInput.cache
- Microsoft.Practices.Composite.Presentation.dll
- Microsoft.Practices.Composite.Presentation.pdb
- Microsoft.Practices.Composite.Presentation.Properties.Resources.resources
- ResGen.read.1.tlog
- ResGen.write.1.tlog
- ResolveAssemblyReference.cache
- TempPE
- Release
- Composite.Presentation.Silverlight.csproj.FileListAbsolute.txt
- Composite.Presentation.Silverlight.csproj.GenerateResource.Cache
- Microsoft.Practices.Composite.Presentation.dll
- Microsoft.Practices.Composite.Presentation.pdb
- Microsoft.Practices.Composite.Presentation.Properties.Resources.resources
- ResolveAssemblyReference.cache
- TempPE
- Properties
- Regions
- Composite
- LIB
- Silverlight
- ServiceLocation
- Microsoft.Practices.ServiceLocation.dll
|
//===================================================================================
// Microsoft patterns & practices
// Composite Application Guidance for Windows Presentation Foundation and Silverlight
//===================================================================================
// Copyright (c) Microsoft Corporation. All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious. No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===================================================================================
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using Microsoft.Practices.Composite.Presentation.Properties;
using Microsoft.Practices.Composite.Regions;
namespace Microsoft.Practices.Composite.Presentation.Regions.Behaviors
{
/// <summary>
/// Defines the attached behavior that keeps the items of the <see cref="Selector"/> host control in synchronization with the <see cref="IRegion"/>.
///
/// This behavior also makes sure that, if you activate a view in a region, the SelectedItem is set. If you set the SelectedItem or SelectedItems (ListBox)
/// then this behavior will also call Activate on the selected items.
/// <remarks>
/// When calling Activate on a view, you can only select a single active view at a time. By setting the SelectedItems property of a listbox, you can set
/// multiple views to active.
/// </remarks>
/// </summary>
public class SelectorItemsSourceSyncBehavior : RegionBehavior, IHostAwareRegionBehavior
{
/// <summary>
/// Name that identifies the SelectorItemsSourceSyncBehavior behavior in a collection of RegionsBehaviors.
/// </summary>
public static readonly string BehaviorKey = "SelectorItemsSourceSyncBehavior";
private bool updatingActiveViewsInHostControlSelectionChanged;
private Selector hostControl;
/// <summary>
/// Gets or sets the <see cref="DependencyObject"/> that the <see cref="IRegion"/> is attached to.
/// </summary>
/// <value>
/// A <see cref="DependencyObject"/> that the <see cref="IRegion"/> is attached to.
/// </value>
/// <remarks>For this behavior, the host control must always be a <see cref="Selector"/> or an inherited class.</remarks>
public DependencyObject HostControl
{
get
{
return this.hostControl;
}
set
{
this.hostControl = value as Selector;
}
}
/// <summary>
/// Starts to monitor the <see cref="IRegion"/> to keep it in synch with the items of the <see cref="HostControl"/>.
/// </summary>
protected override void OnAttach()
{
bool itemsSourceIsSet = this.hostControl.ItemsSource != null;
#if !SILVERLIGHT
itemsSourceIsSet = itemsSourceIsSet || (BindingOperations.GetBinding(this.hostControl, ItemsControl.ItemsSourceProperty) != null);
#endif
if (itemsSourceIsSet)
{
throw new InvalidOperationException(Resources.ItemsControlHasItemsSourceException);
}
this.SynchronizeItems();
this.hostControl.SelectionChanged += this.HostControlSelectionChanged;
this.Region.ActiveViews.CollectionChanged += this.ActiveViews_CollectionChanged;
this.Region.Views.CollectionChanged += this.Views_CollectionChanged;
}
private void Views_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (object newItem in e.NewItems)
{
this.hostControl.Items.Add(newItem);
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (object oldItem in e.OldItems)
{
this.hostControl.Items.Remove(oldItem);
}
}
}
private void SynchronizeItems()
{
List<object> existingItems = new List<object>();
// Control must be empty before "Binding" to a region
foreach (object childItem in this.hostControl.Items)
{
existingItems.Add(childItem);
}
foreach (object view in this.Region.Views)
{
this.hostControl.Items.Add(view);
}
foreach (object existingItem in existingItems)
{
this.Region.Add(existingItem);
}
}
private void ActiveViews_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (this.updatingActiveViewsInHostControlSelectionChanged)
{
// If we are updating the ActiveViews collection in the HostControlSelectionChanged, that
// means the user has set the SelectedItem or SelectedItems himself and we don't need to do that here now
return;
}
if (e.Action == NotifyCollectionChangedAction.Add)
{
if (this.hostControl.SelectedItem != null
&& this.hostControl.SelectedItem != e.NewItems[0]
&& this.Region.ActiveViews.Contains(this.hostControl.SelectedItem))
{
this.Region.Deactivate(this.hostControl.SelectedItem);
}
this.hostControl.SelectedItem = e.NewItems[0];
}
else if (e.Action == NotifyCollectionChangedAction.Remove &&
e.OldItems.Contains(this.hostControl.SelectedItem))
{
this.hostControl.SelectedItem = null;
}
}
private void HostControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
// Record the fact that we are now updating active views in the HostControlSelectionChanged method.
// This is needed to prevent the ActiveViews_CollectionChanged() method from firing.
this.updatingActiveViewsInHostControlSelectionChanged = true;
object source;
#if SILVERLIGHT
// e.OriginalSource == null, that's why we use sender.
source = sender;
#else
source = e.OriginalSource;
#endif
if (source == sender)
{
foreach (object item in e.RemovedItems)
{
// check if the view is in both Views and ActiveViews collections (there may be out of sync)
if (this.Region.Views.Contains(item) && this.Region.ActiveViews.Contains(item))
{
this.Region.Deactivate(item);
}
}
foreach (object item in e.AddedItems)
{
if (this.Region.Views.Contains(item) && !this.Region.ActiveViews.Contains(item))
{
this.Region.Activate(item);
}
}
}
}
finally
{
this.updatingActiveViewsInHostControlSelectionChanged = false;
}
}
}
}
|
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.
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.
Previously Daniel was a nine-time Microsoft MVP and co-founder of
Outcoder, a Swiss software and consulting company.
Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.
Daniel is the developer behind several acclaimed mobile apps including
Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably
Codon.
Would you like Daniel to bring value to your organisation?
Please contact
Blog |
Twitter
Xamarin Experts
Windows 10 Experts