代码之家  ›  专栏  ›  技术社区  ›  cpdev

如何(如果有的话)从Style Trigger IsSelected属性调用VisualStateManager.GotToState

  •  0
  • cpdev  · 技术社区  · 2 年前

    我有VisualStateManager.VisualStateGroups定义如下:

                <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="PopupStates">
                    <VisualState x:Name="PopupClosed">
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames Storyboard.TargetName="ContextPopup"
                                                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="ContextPopup"
                                                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>
    

    在Style声明中,如果单击列表框项(即通过鼠标操作选择),我想调用VisualStateManager.GoToState“PopupClosed”。我找不到VisualStateManager.GoToState可以与触发器组合的示例,例如:

         <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
               ... VisualStateManager.GoToState declaration ....
            </Trigger>
        </Style.Triggers>
    

    有办法做到这一点吗?感谢您的帮助。

    0 回复  |  直到 2 年前
        1
  •  0
  •   BionicCode    2 年前

    不能从XAML调用方法,除非这些方法是事件/命令处理程序(委托)。必须从非XAML上下文显式调用方法。

    VisualStates 通常由定义它们的控件处理。如果这与自定义控件无关,并且您需要更改视觉状态转换行为或触发器,那么最干净的方法是扩展相关控件以私下实现状态转换。
    但是,使用 VisualStateManager.GoToElementState 允许从外部将控件转换为可视状态,例如从附加的 ListBoxItem.Selected 事件处理程序。。

    <ListBox>
      <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
          <EventSetter Event="Selected"
                       Handler="ListBoxItem_Selected" />
        </Style>
      </ListBox.ItemContainerStyle>
    </ListBox>
    
    private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
    {
      var listBoxItem = sender as ListBoxItem;
      var templateChildThatDefinesVisualStateGroups = VisualTreeHelper.GetChild(listBoxItem, 0) as FrameworkElement;
      _ = VisualStateManager.GoToElementState(templateChildThatDefinesVisualStateGroupshild, "PopupClosed", true);
    }
    
    推荐文章