mijikin
mijikin

Reputation: 101

How to set event trigger on toolkit DropDownButton IsOpen

I have a DropDownButton (a component from the Extended WPF Toolkit) that has a ListView as dropdown content. I would like to close the Popup part as soon as the user selects an item. I thought I could achieve this through an event trigger with ListView.SelectionChanged as source event, and DropDownButton.IsOpen as target property which I would set to false.

But then I'm getting the following exception when I select an item:

Cannot resolve all property references in the property path 'IsOpen'. Verify that applicable objects support the properties.

Here is my XAML:

<ListView.Triggers>
<EventTrigger RoutedEvent="ListView.SelectionChanged" SourceName="MyListView">
    <EventTrigger.Actions>
        <BeginStoryboard>
            <Storyboard>
                <BooleanAnimationUsingKeyFrames
                Storyboard.Target="{Binding Source={RelativeSource Mode=FindAncestor,AncestorType=xctk:DropDownButton}}"
                Storyboard.TargetProperty="IsOpen"
                FillBehavior="HoldEnd">
                    <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:1" />
                </BooleanAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.Actions>
</EventTrigger>
</ListView.Triggers>

Can anybody tell me what I'm doing wrong?

Upvotes: 1

Views: 603

Answers (1)

mijikin
mijikin

Reputation: 101

For those interested, here is a relatively simple solution that does not involve any code-behind.

<Window x:Class="PopupDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style x:Key="MyPopupMenu"  TargetType="MenuItem"  >
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="MenuItem">
                        <Border x:Name="Border"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid >
                                <ContentPresenter ContentSource="Header" />
                                <Rectangle x:Name="overlay"
                                               Fill="Black"
                                               IsHitTestVisible="False"
                                               Visibility="Hidden"
                                               Opacity="0.1" >
                                </Rectangle>

                                <Popup IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" x:Name="SubMenuPopup" Focusable="false" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}">
                                    <Border x:Name="SubMenuBorder" 
                                            Background="White"
                                            BorderBrush="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Menu}}}" 
                                            BorderThickness="1" Padding="2,2,2,2">
                                        <Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True">
                                            <!-- StackPanel holds children of the menu. This is set by IsItemsHost=True -->
                                            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/>
                                        </Grid>
                                    </Border>
                                </Popup>

                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver"  Value="True" >
                                <Setter TargetName="overlay" Property="Visibility" Value="Visible" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>


    <DockPanel LastChildFill="False">
        <Menu DockPanel.Dock="Top"  Background="Transparent">
            <MenuItem Style="{StaticResource MyPopupMenu}" >
                <MenuItem.Header>
                    <Border Background="#3382cc" BorderThickness="12 4" BorderBrush="#3382cc">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="My Popup Menu"  Foreground="White"  />
                        </StackPanel>
                    </Border>
                </MenuItem.Header>
                <MenuItem Header="Alfa"  />
                <MenuItem Header="Bravo"  />
                <MenuItem Header="Charlie"  />
                <MenuItem Header="Delta"  />
                <MenuItem Header="Echo"  />
            </MenuItem>
        </Menu>
    </DockPanel>
</Window>

Upvotes: 1

Related Questions