在 xaml 中创建自定义 VisualState 并在 CodeBehind 中手动设置

发布于 2024-12-10 18:34:11 字数 508 浏览 0 评论 0原文

我有一个 TabItem 样式,其中有 VisualStates。

<VisualState x:Name="MouseOver"> 
<!-- Tab turns bronze when mouseover -->
</VisualState>

现在我想要一个自定义的视觉状态,并在代码隐藏中手动设置状态,而不是依赖 MouseOver 事件。

<VisualState x:Name="CustomVisualState">
<!-- this will be a storyboard to cause flashing -->
</VisualState> 

然后我需要在CodeBehind中设置它。

MyTabItem.VisualState = CustomVisualState.  //something like this

I have a TabItem style, which has VisualStates.

<VisualState x:Name="MouseOver"> 
<!-- Tab turns bronze when mouseover -->
</VisualState>

Now I want to have a custom visual state and manually set the state in codebehind instead of relying on the MouseOver event.

<VisualState x:Name="CustomVisualState">
<!-- this will be a storyboard to cause flashing -->
</VisualState> 

Then I need to set it in CodeBehind.

MyTabItem.VisualState = CustomVisualState.  //something like this

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

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

发布评论

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

评论(3

因为看清所以看轻 2024-12-17 18:34:11

你尝试过吗
VisualStateManager.GoToState

获取一个 Control、带有自定义状态名称的字符串以及用于使用转换的 bool 标志。

用法

private void UpdateStates(bool useTransitions)
{
    if (Value >= 0)
    {
        VisualStateManager.GoToState(this, "Positive", useTransitions);
    }
    else
    {
        VisualStateManager.GoToState(this, "Negative", useTransitions);
    }

    if (IsFocused)
    {
        VisualStateManager.GoToState(this, "Focused", useTransitions);
    }
    else
    {
        VisualStateManager.GoToState(this, "Unfocused", useTransitions);
    }
}

来自 msdn 的示例 稍微复杂一点的示例用法来自 此处

鉴于此 xaml

      <Grid x:Name="LayoutRoot"  Background="LightBlue">
          <VisualStateManager.VisualStateGroups>
              <VisualStateGroup x:Name="SG1">
                  <VisualStateGroup.Transitions>
                      <VisualTransition GeneratedDuration="00:00:01">
                          <VisualTransition.GeneratedEasingFunction>
                              <ElasticEase EasingMode="EaseOut"/>
                          </VisualTransition.GeneratedEasingFunction>
                      </VisualTransition>
                  </VisualStateGroup.Transitions>
                  <VisualState x:Name="SG1Normal"/>
                  <VisualState x:Name="SG1EllipseRight" >
                      <Storyboard>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                              <EasingDoubleKeyFrame KeyTime="00:00:00" Value="320"/>
                          </DoubleAnimationUsingKeyFrames>
                      </Storyboard>
                  </VisualState>
              </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Ellipse x:Name="ellipse" Fill="Red" Stroke="Black" 
                   Height="116" HorizontalAlignment="Left" Margin="50,98,0,0" 
                   VerticalAlignment="Top" Width="235" RenderTransformOrigin="0.5,0.5" >
              <Ellipse.RenderTransform>
                  <TransformGroup>
                      <ScaleTransform/>
                      <SkewTransform/>
                      <RotateTransform/>
                      <TranslateTransform/>
                  </TransformGroup>
              </Ellipse.RenderTransform>
          </Ellipse>
      </Grid>

可以像这样改变状态。

 VisualStateManager.GoToState(this, SG1EllipseRight.Name, true);

或者

VisualStateManager.GoToState(control, "SG1EllipseRight", true);

Have you tried
VisualStateManager.GoToState

Takes a Control, string with the custom state name and a bool flag for using transitions.

Example Usage From msdn

private void UpdateStates(bool useTransitions)
{
    if (Value >= 0)
    {
        VisualStateManager.GoToState(this, "Positive", useTransitions);
    }
    else
    {
        VisualStateManager.GoToState(this, "Negative", useTransitions);
    }

    if (IsFocused)
    {
        VisualStateManager.GoToState(this, "Focused", useTransitions);
    }
    else
    {
        VisualStateManager.GoToState(this, "Unfocused", useTransitions);
    }
}

A slightly more complicated example usage from here

Given this xaml

      <Grid x:Name="LayoutRoot"  Background="LightBlue">
          <VisualStateManager.VisualStateGroups>
              <VisualStateGroup x:Name="SG1">
                  <VisualStateGroup.Transitions>
                      <VisualTransition GeneratedDuration="00:00:01">
                          <VisualTransition.GeneratedEasingFunction>
                              <ElasticEase EasingMode="EaseOut"/>
                          </VisualTransition.GeneratedEasingFunction>
                      </VisualTransition>
                  </VisualStateGroup.Transitions>
                  <VisualState x:Name="SG1Normal"/>
                  <VisualState x:Name="SG1EllipseRight" >
                      <Storyboard>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                              <EasingDoubleKeyFrame KeyTime="00:00:00" Value="320"/>
                          </DoubleAnimationUsingKeyFrames>
                      </Storyboard>
                  </VisualState>
              </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Ellipse x:Name="ellipse" Fill="Red" Stroke="Black" 
                   Height="116" HorizontalAlignment="Left" Margin="50,98,0,0" 
                   VerticalAlignment="Top" Width="235" RenderTransformOrigin="0.5,0.5" >
              <Ellipse.RenderTransform>
                  <TransformGroup>
                      <ScaleTransform/>
                      <SkewTransform/>
                      <RotateTransform/>
                      <TranslateTransform/>
                  </TransformGroup>
              </Ellipse.RenderTransform>
          </Ellipse>
      </Grid>

Changing state can be done like so.

 VisualStateManager.GoToState(this, SG1EllipseRight.Name, true);

Or alternatively

VisualStateManager.GoToState(control, "SG1EllipseRight", true);
无可置疑 2024-12-17 18:34:11

试试这个,

VisualStateManager.GoToElementState(Control, "StateName", true/false);

或者

VisualStateManager.GoToState(Control, "StateName", true/false);

Try this,

VisualStateManager.GoToElementState(Control, "StateName", true/false);

or

VisualStateManager.GoToState(Control, "StateName", true/false);
無處可尋 2024-12-17 18:34:11

VisualStateManager 还允许您指定控件何时
进入特定状态。您应该调用来更改的方法
状态取决于您的场景。如果您创建一个使用
VisualStateManager 在其ControlTemplate 中,调用GoToState 方法。
有关如何创建使用以下控件的更多信息
VisualStateManager,请参阅创建具有可自定义的控件
外貌。如果您在外部使用 VisualStateManager
ControlTemplate(例如,如果您在
UserControl 或单个元素中),调用 GoToElementState 方法。
无论哪种情况,VisualStateManager 执行的逻辑是
需要适当地启动和停止故事板
与所涉及的状态相关联。 - VisualStateManager 类

这就是 GoToElementStateGoToState 之间的区别。

The VisualStateManager also enables you to specify when a control
enters a specific state. The method that you should call to change
states depends on your scenario. If you create a control that uses the
VisualStateManager in its ControlTemplate, call the GoToState method.
For more information about how to create controls that use the
VisualStateManager, see Creating a Control That Has a Customizable
Appearance. If you use the VisualStateManager outside of a
ControlTemplate (for example, if you use a VisualStateManager in a
UserControl or in a single element), call the GoToElementState method.
In either case, the VisualStateManager performs the logic that is
required to appropriately start and stop the storyboards that are
associated with the involved state. - VisualStateManager Class

That is how different between GoToElementState and GoToState.

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