Select to view content in your preferred language

Problem with Drop Down menu class

930
2
09-24-2012 02:23 PM
BrianMulcahy
Occasional Contributor
I am currently using the drop down menu class in my application to sort different menus.

In one of my drop down menus I have a combo box that I want a user to be able to select a value from.
Currently if a user selects the drop down box, and then tries to select a value, the Drop Down menu class will fire the mouse leave event which will close the menu.

Is there a way to override the Mouse Leave event while the combo box is open?

Here is the code for the DropDownMenu Class
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;

namespace ESRI.ArcGIS.SilverlightMapApp
{
    /// <summary>
    /// Resizable and draggable custom window control
    /// </summary>
    public class DropDownMenu : ContentControl
    {
        public DropDownMenu()
        {
            DefaultStyleKey = typeof(DropDownMenu);
            this.MouseEnter += DropDownMenu_MouseEnter;
            this.MouseLeave += DropDownMenu_MouseLeave;
            
        }
              

        private void DropDownMenu_MouseLeave(object sender, MouseEventArgs e)
        {
            GoToState(true, "Hidden");
        }

        private void DropDownMenu_MouseEnter(object sender, MouseEventArgs e)
        {
            GoToState(true, "Visible");
        }


        private bool GoToState(bool useTransitions, string stateName)
        {
            return VisualStateManager.GoToState(this, stateName, useTransitions);
        }

        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            if (oldContent != null && oldContent is UIElement)
            {
                (oldContent as UIElement).MouseEnter -= DropDownMenu_MouseEnter;
                (oldContent as UIElement).MouseLeave -= DropDownMenu_MouseLeave;
            }
            if (newContent != null && newContent is UIElement)
            {
                (newContent as UIElement).MouseEnter += DropDownMenu_MouseEnter;
                (newContent as UIElement).MouseLeave += DropDownMenu_MouseLeave;
            }
        }

        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or
        /// internal processes (such as a rebuilding layout pass) call
        /// <see cref="M:System.Windows.Controls.Control.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            bool isDesignMode = System.ComponentModel.DesignerProperties.GetIsInDesignMode(this);
            GoToState(false, isDesignMode ? "Visible" : "Hidden"); //Show submenu when in designmode
        }

        /// <summary>
        /// Identifies the <see cref="MenuContent"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty MenuHeaderProperty =
                        DependencyProperty.Register("MenuHeader", typeof(object), typeof(DropDownMenu), null);

        /// <summary>
        /// Gets or sets MenuContent.
        /// </summary>
        public object MenuHeader
        {
            get { return (object)GetValue(MenuHeaderProperty); }
            set { SetValue(MenuHeaderProperty, value); }
        }

        /// <summary>
        /// Gets or sets the template that is used to display the content of the
        /// control's header.
        /// </summary>
        /// <value>
        /// The template that is used to display the content of the control's
        /// header. The default is null.
        /// </value>
        public DataTemplate MenuHeaderTemplate
        {
            get { return (DataTemplate)GetValue(MenuHeaderTemplateProperty); }
            set { SetValue(MenuHeaderTemplateProperty, value); }
        }

        /// <summary>
        /// Identifies the <see cref="HeaderTemplate" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty MenuHeaderTemplateProperty =
                DependencyProperty.Register(
                        "MenuHeaderTemplate",
                        typeof(DataTemplate),
                        typeof(DropDownMenu), null);

    }
}


Here is my xaml(partial for the sake of length) in my main for the menu and its' nested combo box.
 <userControls:DropDownMenu VerticalAlignment="Center">
                                        <userControls:DropDownMenu.MenuHeader>
                                            <Button Margin="0,0,2,0" Cursor="Arrow" Height="30">
                                                <Button.Content>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="Imagery" Foreground="#FF6A7A89" FontWeight="Bold" />

                                                    </StackPanel>
                                                </Button.Content>
                                            </Button>
                                        </userControls:DropDownMenu.MenuHeader>
                                        <Border Style="{StaticResource CommonBorder}">
                                            <Border.Effect>
                                                <DropShadowEffect Color="Black" Direction="-45" BlurRadius="20" Opacity=".75" />
                                            </Border.Effect>
                                            <StackPanel>
                                                <toolkit:Accordion HorizontalAlignment="Left"  Height="Auto" Width="Auto" SelectionMode="ZeroOrMore" >
                                                    <toolkit:AccordionItem>
                                                        <toolkit:AccordionItem.Header>
                                                            <TextBlock Text="Landsat"/>
                                                        </toolkit:AccordionItem.Header>
                                                        <toolkit:AccordionItem.Content>
                                                            <StackPanel>
                                                                <CheckBox 
                                                                          ToolTipService.ToolTip="Landsat5 TM Natural Color"
                                                                          Click="Landsat5NC_Click" >
                                                                    <ComboBox x:Name="Landsat7Tmlist" Width="auto">
                                                                        <ComboBox.ItemTemplate>
                                                                            <DataTemplate>

                                                                                <TextBlock Text="{Binding Name}"/>

                                                                            </DataTemplate>
                                                                        </ComboBox.ItemTemplate>
                                                                    </ComboBox>

                                                                </CheckBox>
                                                                <CheckBox Content="Landsat7 ETM+ NC" 
                                                                          ToolTipService.ToolTip="Landsat7 ETM+ Natural Color - Aug 27 2012"
                                                                          Click="Landsat7ETMNC_Click" >
                                                                    

                                                                </CheckBox>
                                                            </StackPanel>
                                                        </toolkit:AccordionItem.Content>
                                                    </toolkit:AccordionItem>
 </toolkit:Accordion>



                                            </StackPanel>
                                        </Border>
                                    </userControls:DropDownMenu>
0 Kudos
2 Replies
BrianMulcahy
Occasional Contributor
I had an idea that if I were able to get the ComboBox.IsDropDownOpen property to work in that class I could use a simple if statement but I can't figure out how to get the combobox property into this class.
0 Kudos
BrianMulcahy
Occasional Contributor
I figured it out just added to the Mouseleave event in the dropdownmenu class

 private void DropDownMenu_MouseLeave(object sender, MouseEventArgs e)
        {
            MainPage mp = (MainPage)Application.Current.RootVisual;

           if( mp.Landsat7Tmlist.IsDropDownOpen == false )
            {
                GoToState(true, "Hidden");
            }


        }
0 Kudos