- ComboBox - TreeView, close popup on item select?
- <Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
- <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}">
- <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
- <ScrollViewer>
- <TreeView x:Name="PART_TreeView" ItemsSource="{TemplateBinding ItemsSource}">
- <Interactivity:Interaction.Behaviors>
- <ComboTreeView:BindableSelectedItemBehaviour SelectedItem="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox} }, Path=SelectedItem, Mode=TwoWay}" />
- </Interactivity:Interaction.Behaviors>
- </TreeView>
- </ScrollViewer>
- </Border>
- </Microsoft_Windows_Themes:SystemDropShadowChrome>
- </Popup>
- private void OnTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
- {
- this.SelectedItem = e.NewValue;
- var treeView = (TreeView)sender;
- var control = (FrameworkElement)treeView.TemplatedParent;
- ComboBox combo;
- do
- {
- combo = control as ComboBox;
- if (combo != null)
- {
- break;
- }
- }
- while ((control = (FrameworkElement)control.TemplatedParent) != null);
- if (combo == null)
- {
- return;
- }
- Dispatcher.BeginInvoke(new Action(() => combo.IsDropDownOpen = false));
- }
- void EnsureComboPopupClosed(ComboBox cb)
- {
- if (cb == null || cb.Template == null)
- return;
- Popup popup = cb.Template.FindName("PART_Popup", cb) as Popup;
- if (popup == null)
- return;
- popup.IsOpen = false;
- }
- ... xmlns:s="clr-namespace:System;assembly=mscorlib" ...
- ...
- <ControlTemplate.Resources>
- ....
- <Storyboard x:Key="ClosePopup"
- Duration="0:0:0"
- Storyboard.TargetName="PART_Popup"
- Storyboard.TargetProperty="IsOpen" >
- <ObjectAnimationUsingKeyFrames>
- <DiscreteObjectKeyFrame KeyTime="0:0:0">
- <DiscreteObjectKeyFrame.Value>
- <s:Boolean>False</s:Boolean>
- </DiscreteObjectKeyFrame.Value>
- </DiscreteObjectKeyFrame>
- </ObjectAnimationUsingKeyFrames>
- </Storyboard>
- ...
- </ControlTemplate.Resources>
- ...
- <TreeView x:Name="PART_TreeView" ... >
- ...
- <TreeView.Triggers>
- <EventTrigger RoutedEvent="TreeView.SelectedItemChanged">
- <EventTrigger.Actions>
- ...
- <BeginStoryboard Storyboard="{StaticResource ClosePopup}"/>
- </EventTrigger.Actions>
- </EventTrigger>
- </TreeView.Triggers>
- ...
- </TreeView>
- ...