VisualStateManager.GoToState 方法调用不改变状态并显示弹出窗口

问题描述 投票:0回答:0

我有一个由如下所示的 ControlTemplate 定义的控件:

    <ControlTemplate x:Key="UXIconComboBoxControlTemplate" TargetType="{x:Type controls:UXIconComboBox}" >
    <ControlTemplate.Resources>
        <converters:IconComboBoxDropdownHorizontalOffsetMultiConverter x:Key="ComboBoxDropdownHorizontalOffsetConverter"/>
    </ControlTemplate.Resources>
    <Grid 
        x:Name="rootGrid"
        Width="{TemplateBinding HitAreaWidth}" 
        Height="{TemplateBinding HitAreaHeight}" 
        Background="{TemplateBinding Background}"
        >
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <controls:ExtendedHitAreaButton 
            Grid.Row="0"
            x:Name="PopupExpanderButton"  << Popup Button
            IsTabStop="True"
            AutomationProperties.AutomationId="UXIconComboBox"
            Style="{StaticResource PopupExpanderButtonStyle}">
        </controls:ExtendedHitAreaButton>
        <Popup 
            x:Name="IconComboBoxPopup"
            Grid.Row="1" 
            AutomationProperties.AutomationId="UXIconComboBoxPopup"
            Placement="Bottom"
            PlacementTarget="{Binding ElementName=PopupExpanderButton}"
            MinWidth="200"
            StaysOpen="False">
            <Popup.HorizontalOffset>
                <MultiBinding Converter="{StaticResource ComboBoxDropdownHorizontalOffsetConverter}">
                    <Binding ElementName="PopupExpanderButton" Path="ActualWidth"/>
                    <Binding ElementName="IconComboBoxPopup_ListBox" Path="ActualWidth"/>
                </MultiBinding>
            </Popup.HorizontalOffset>
            <ListBox 
                x:Name="IconComboBoxPopup_ListBox" 
                AutomationProperties.AutomationId="UXIconComboBoxListBox"
                ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource TemplatedParent}}"
                IsSynchronizedWithCurrentItem="True"
                Focusable="True"
                KeyboardNavigation.TabNavigation="Cycle"
                KeyboardNavigation.DirectionalNavigation="Cycle"    
                BorderThickness="0"
                ItemContainerStyle="{StaticResource ListBoxContainerStyle}"
                ItemTemplate="{Binding ItemTemplate, RelativeSource={RelativeSource TemplatedParent}}">
            </ListBox>

        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="PopupStates">
                <VisualState x:Name="PopupClosed">
                    <Storyboard>
                        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="IconComboBoxPopup"
                                            Storyboard.TargetProperty="IsOpen">
                            <DiscreteBooleanKeyFrame KeyTime="0:0:0.25"
                                       Value="False" />
                        </BooleanAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="rootGrid"
                                        Storyboard.TargetProperty="(Panel.Background)">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0.25" Value="{DynamicResource BrushIconComboBox_Popup_Closed_Background}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="PopupOpen">
                    <Storyboard>
                        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="IconComboBoxPopup"
                                            Storyboard.TargetProperty="IsOpen">
                            <DiscreteBooleanKeyFrame KeyTime="0:0:0.25"
                                       Value="True" />
                        </BooleanAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="rootGrid"
                                        Storyboard.TargetProperty="(Panel.Background)">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0.25" Value="{DynamicResource BrushIconComboBox_Popup_Open_Background}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        </Popup>
    </Grid>
</ControlTemplate>

我还有一个 PopupExpanderButton 的 Click 事件处理程序,如下所示:

    /// <summary>
/// Handler for the ExtendedHitAreaButton click event
/// </summary>
/// <param name="sender">listBoxItem info</param>
/// <param name="e">routed event info</param>
public void PopupExpanderButton_Click(object sender, RoutedEventArgs e)
{
    Debug.WriteLine($"PopupExpanderButton_Click called - popup state {IconComboBoxPopup.IsOpen}");

    var popupState = IconComboBoxPopup.IsOpen ? PopupClosed : PopupOpen;

    var result = VisualStateManager.GoToState(this, popupState, true);

    e.Handled = true;
}

如果我点击 PopupExpanderButton,将执行上面的 Click 事件并显示 Popup 窗口。请注意,PopupExpanderButton 只是 Button 的一个子类,它包含一个调用 PointHitTestResult 的 HitTestCore 函数。

在不将鼠标从 PopupExpanderButton 上移开的情况下,如果我再次单击 PopupExpanderButton,弹出窗口将获得一个失去焦点事件并正常关闭,但随后会执行上面的 Click 事件,IconComboBoxPopup.IsOpen 为 false, GoToState 是使用 PopupOpen 值执行的....但弹出窗口不会打开。 GoToState 调用的结果值始终为 True。将方法更改为 GoToElementState 没有任何区别。

如果我一直点击 PopupExpanderButton,上面的 Click 事件会以 PopupOpen 值执行,但是 Popup 窗口不会再次打开。

谁能告诉我为什么 GoToState 方法在第一次点击事件后不起作用?

提前致谢!

wpf xaml visualstatemanager
© www.soinside.com 2019 - 2024. All rights reserved.