XAML 如果可以,如何从样式触发器IsSelected属性调用VisualStateManager.GotToState

j8yoct9x  于 2023-04-27  发布在  Go
关注(0)|答案(1)|浏览(106)

我将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>

有什么方法可以做到这一点吗?任何帮助都很感激。

kx5bkwkv

kx5bkwkv1#

不能从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);
}

相关问题