n 秒后关闭组合框

发布于 2024-09-24 13:21:26 字数 4458 浏览 1 评论 0原文

我有一个组合框,如果用户未进行选择,该组合框将保持打开状态。我想使用触发器在 2 秒后关闭组合框。这是我的组合框样式的一部分,其中包括我完成此任务失败的事件触发尝试:

 <Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">
   <Setter Property="FontFamily" Value="Bryant"/>
   <Setter Property="FontSize" Value="18px"/>
   <Setter Property="FontWeight" Value="Bold"/>
   <Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}"/>
   <Setter Property="Foreground" Value="#FF000000"/>
   <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
   <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
   <Setter Property="Padding" Value="4,3"/>
   <Setter Property="Margin" Value="5"/>
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type ComboBox}">
      <Grid x:Name="MainGrid" SnapsToDevicePixels="true">
       <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0*"/>
        <ColumnDefinition/>
        <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
       </Grid.ColumnDefinitions>
       <Popup Margin="1" x:Name="PART_Popup" AllowsTransparency="true" StaysOpen="False" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Grid.ColumnSpan="2" Grid.Column="1">
         <Border x:Name="DropDownBorder" Background="{DynamicResource GrayBG}" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}" BorderBrush="{StaticResource GrayBGBorder}" BorderThickness="2,0,2,2" CornerRadius="0,0,4,4" Width="{Binding ActualWidth, ElementName=DropWidth}">
         <ScrollViewer CanContentScroll="true" Template="{DynamicResource NeeboScrollViewer}">
          <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Contained"/>
         </ScrollViewer>
        </Border>
       </Popup>
       <ToggleButton x:Name="DropWidth" Style="{StaticResource ComboBoxReadonlyToggleButton}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="1"/>
       <ContentPresenter HorizontalAlignment="Left" Margin="35,0,0,0" VerticalAlignment="Center" IsHitTestVisible="false" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Grid.Column="1"/>
      </Grid>
      <ControlTemplate.Triggers>
       <Trigger Property="HasItems" Value="false">
        <Setter Property="Height" TargetName="DropDownBorder" Value="95"/>
       </Trigger>
       <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        <Setter Property="Background" Value="#FFF4F4F4"/>
       </Trigger>
       <Trigger Property="IsGrouping" Value="true">
        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
       </Trigger>
                            <EventTrigger RoutedEvent="(Popup.Opened)"  SourceName="PART_Popup" >
                                <BeginStoryboard>
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_Popup" Storyboard.TargetProperty="(Popup.IsOpen)">
                                            <DiscreteBooleanKeyFrame KeyTime="00:00:03"  Value="False"/>
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </ControlTemplate.Triggers>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>

有关如何完成此任务的任何建议?

I have a combobox that stays open if the user does not make a selection. I'd like to use a trigger to close the combobox after 2 seconds. Here is a portion of my combobox style that includes my failed eventtrigger attempt at accomplishing this:

 <Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">
   <Setter Property="FontFamily" Value="Bryant"/>
   <Setter Property="FontSize" Value="18px"/>
   <Setter Property="FontWeight" Value="Bold"/>
   <Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}"/>
   <Setter Property="Foreground" Value="#FF000000"/>
   <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
   <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
   <Setter Property="Padding" Value="4,3"/>
   <Setter Property="Margin" Value="5"/>
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type ComboBox}">
      <Grid x:Name="MainGrid" SnapsToDevicePixels="true">
       <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0*"/>
        <ColumnDefinition/>
        <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
       </Grid.ColumnDefinitions>
       <Popup Margin="1" x:Name="PART_Popup" AllowsTransparency="true" StaysOpen="False" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Grid.ColumnSpan="2" Grid.Column="1">
         <Border x:Name="DropDownBorder" Background="{DynamicResource GrayBG}" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}" BorderBrush="{StaticResource GrayBGBorder}" BorderThickness="2,0,2,2" CornerRadius="0,0,4,4" Width="{Binding ActualWidth, ElementName=DropWidth}">
         <ScrollViewer CanContentScroll="true" Template="{DynamicResource NeeboScrollViewer}">
          <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Contained"/>
         </ScrollViewer>
        </Border>
       </Popup>
       <ToggleButton x:Name="DropWidth" Style="{StaticResource ComboBoxReadonlyToggleButton}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="1"/>
       <ContentPresenter HorizontalAlignment="Left" Margin="35,0,0,0" VerticalAlignment="Center" IsHitTestVisible="false" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Grid.Column="1"/>
      </Grid>
      <ControlTemplate.Triggers>
       <Trigger Property="HasItems" Value="false">
        <Setter Property="Height" TargetName="DropDownBorder" Value="95"/>
       </Trigger>
       <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        <Setter Property="Background" Value="#FFF4F4F4"/>
       </Trigger>
       <Trigger Property="IsGrouping" Value="true">
        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
       </Trigger>
                            <EventTrigger RoutedEvent="(Popup.Opened)"  SourceName="PART_Popup" >
                                <BeginStoryboard>
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_Popup" Storyboard.TargetProperty="(Popup.IsOpen)">
                                            <DiscreteBooleanKeyFrame KeyTime="00:00:03"  Value="False"/>
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </ControlTemplate.Triggers>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>

Any suggestions on how to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋 2024-10-01 13:21:26

我想不出一种方法,可以在不实现附加行为的情况下执行此操作,该附加行为将订阅打开的事件并等待 2 秒来关闭它(如果尚未关闭)。

如果你不知道什么是附加行为,只需谷歌一下即可。

实现后,您可以在您的样式中将其设置为默认行为。

I can't think of a way doing this without implement attached behavior that will subscribe the opened event and wait 2 seconds to close it if not closed already.

If you don't know what is attached behavior just google it.

After you'll implement one you can set it in your style as default behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文