Microsoft.Phone.Applications.UnitConverter.View.CategorySelection.OnFavoritesSelectionChanged C# (CSharp) Method

OnFavoritesSelectionChanged() private method

Called when a favorites selection changes
private OnFavoritesSelectionChanged ( object sender, System.Windows.Controls.SelectionChangedEventArgs e ) : void
sender object The sender.
e System.Windows.Controls.SelectionChangedEventArgs Event data.
return void
        private void OnFavoritesSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (contextMenuPanel.IsOpen == false)
            {
                this.viewModel.ProcessFavoriteSelectionChanged(e);
            }
            else
            {
                /* There is a small race condition between the selected event and when 
                 * the context menu gets events and takes control.
                 * A problem can occur that if an item in the listbox is not selected,
                 * on the mouse up event, the item can get selected even if the context
                 * menu is up. This is not the desired behavior.
                 * This can also cause a current selected item to switch incorrectly.
                 * To work around this problem, we force the list box selection to either 
                 * be no selection if we did not have an item currently selected, or back
                 * to the correct item if one was already selected.
                 */
                ListBox lb = sender as ListBox;
                if (lb != null)
                {
                    if (this.viewModel.lastSelectedFavorite == null)
                    {
                        lb.SelectedIndex = -1;
                    }
                    else
                    {
                        lb.SelectedItem = this.viewModel.lastSelectedFavorite;
                    }
                }
            }
        }