/// <summary>
/// Change whether a TreeViewItem is selected.
/// </summary>
/// <param name="itemOrContainer">
/// Item whose selection is changing.
/// </param>
/// <param name="container">
/// Container of the item whose selection is changing.
/// </param>
/// <param name="selected">
/// A value indicating whether the TreeViewItem is selected.
/// </param>
internal void ChangeSelection(object itemOrContainer, TreeViewItem container, bool selected)
{
// Ignore any change notifications if we're alread in the middle of
// changing the selection
if (IsSelectionChangeActive)
{
return;
}
object oldValue = null;
object newValue = null;
bool raiseSelectionChanged = false;
TreeViewItem element = SelectedContainer;
// Start changing the selection
IsSelectionChangeActive = true;
try
{
if (selected && container != SelectedContainer)
{
// Unselect the old value
oldValue = SelectedItem;
if (SelectedContainer != null)
{
SelectedContainer.IsSelected = false;
SelectedContainer.UpdateContainsSelection(false);
}
// Select the new value
newValue = itemOrContainer;
SelectedContainer = container;
SelectedContainer.UpdateContainsSelection(true);
SelectedItem = itemOrContainer;
UpdateSelectedValue(itemOrContainer);
raiseSelectionChanged = true;
// Scroll the selected item into view. We only want to
// scroll the header into view, if possible, because an
// expanded TreeViewItem contains all of its child items
// as well.
ItemsControlHelper.ScrollIntoView(container.HeaderElement ?? container);
}
else if (!selected && container == SelectedContainer)
{
// Unselect the old value
SelectedContainer.UpdateContainsSelection(false);
SelectedContainer = null;
SelectedItem = null;
SelectedValue = null;
oldValue = itemOrContainer;
raiseSelectionChanged = true;
}
container.IsSelected = selected;
}
finally
{
// Finish changing the selection
IsSelectionChangeActive = false;
}
// Notify when the selection changes
if (raiseSelectionChanged)
{
if (SelectedContainer != null && AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementSelected))
{
AutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(SelectedContainer);
if (peer != null)
{
peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementSelected);
}
}
if (element != null && AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection))
{
AutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(element);
if (peer != null)
{
peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection);
}
}
OnSelectedItemChanged(new RoutedPropertyChangedEventArgs <object>(oldValue, newValue));
}
}