Focus或GotFocus上的WPF Open Combobox弹出窗口

时间:2010-03-08 13:47:36

标签: wpf combobox popup focus

我正在尝试使用样式/事件触发器

进行聚焦时打开组合框弹出窗口

我在Combobox控件模板中使用了以下代码:

<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
        <Grid >
            <ToggleButton Grid.Column="2" Template="{DynamicResource ComboBoxToggleButton}" x:Name="ToggleButton" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
            <ContentPresenter  HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="ContentSite" VerticalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" IsHitTestVisible="False"/>
            <TextBox Visibility="Hidden" Template="{DynamicResource ComboBoxTextBox}" HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="PART_EditableTextBox" Style="{x:Null}" VerticalAlignment="Center"  Focusable="True" Background="Transparent" IsReadOnly="{TemplateBinding IsReadOnly}"/>
            <Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide">
                <Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True">
                    <Border x:Name="DropDownBorder" Background="{DynamicResource ShadeBrush}" BorderBrush="{DynamicResource SolidBorderBrush}" BorderThickness="1"/>
                    <ScrollViewer Margin="4,6,4,6"   SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True">
                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/>
                    </ScrollViewer>
                </Grid>
            </Popup>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver"  Value="True">
                <Setter Property="IsOpen" Value="True" TargetName="Popup"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

但这段代码没有任何反应。 所以我在ControlTemplate.Triggers

中设置了以下事件触发器
<EventTrigger RoutedEvent="UIElement.GotFocus">
                <BeginStoryboard>
                    <Storyboard >
                        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="Popup" Storyboard.TargetProperty="IsOpen" FillBehavior="HoldEnd">
                            <DiscreteBooleanKeyFrame KeyTime="00:00:00"  Value="True" />
                        </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="UIElement.LostFocus">
                <BeginStoryboard>
                    <Storyboard >
                        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="Popup" Storyboard.TargetProperty="IsOpen" FillBehavior="HoldEnd">
                            <DiscreteBooleanKeyFrame KeyTime="00:00:00"  Value="False" />
                        </BooleanAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>

现在这有助于打开焦点上的弹出窗口,但是当我从下拉列表中选择任何项目时,弹出窗口不会像往常一样消失;它保持开放。它只在我点击窗口中的其他位置后关闭。有人可以建议正确的方法来做到这一点 提前致谢

0 个答案:

没有答案