Thursday, February 10, 2011

Combo Box drop down on Mouse Leave/ Mouse Enter in XAML

Want your combobox to drop when you hover on it? And hide items when leave combobox? You are at the right place to find the answer.

Download sample project here.

Achieving this is not straight forward. You need to do some tweaking with the Combox style itself. In Expression Blend create a copy of the combobox template and rename it to comboboxstyle

image

You see a template structure created. Below are the parts.

image

So, you need On MainGrid MouseEnter = Popup.Open; On MainGrid.MouseLeave = Popup.Close. Below is the code which does that.

<Grid x:Name="MainGrid" SnapsToDevicePixels="true">
<
Grid.ColumnDefinitions
>
<
ColumnDefinition Width
="*"/>
<
ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width
="0"/>
</
Grid.ColumnDefinitions
>
<
i:Interaction.Triggers>
<
i:EventTrigger EventName
="MouseEnter">
<
ei:ChangePropertyAction TargetObject="{Binding ElementName=PART_Popup}" PropertyName="IsOpen" Value
="True"/>
</
i:EventTrigger
>
<
i:EventTrigger EventName
="MouseLeave">
<
ei:ChangePropertyAction TargetObject="{Binding ElementName=PART_Popup}" PropertyName="IsOpen" Value
="False"/>
</
i:EventTrigger
>
</
i:Interaction.Triggers
>
<
Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
<
Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName
=MainGrid}">
<
Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors
.WindowBrushKey}}">
<
ScrollViewer x:Name
="DropDownScrollViewer">
<
Grid RenderOptions.ClearTypeHint
="Enabled">
<
Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width
="0">
<
Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName
=DropDownBorder}"/>
</
Canvas
>
<
ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels
}"/>
</
Grid
>
</
ScrollViewer
>
</
Border
>
</
Microsoft_Windows_Themes:SystemDropShadowChrome
>
</
Popup
>
<
ToggleButton BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxReadonlyToggleButton
}"/>
<
ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment
}"/>
</
Grid
>

No comments: