gpt4 book ai didi

wpf - 在xaml中创建自定义VisualState并在CodeBehind中手动进行设置

转载 作者:行者123 更新时间:2023-12-04 13:20:44 25 4
gpt4 key购买 nike

我有一个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

最佳答案

你有没有尝试过VisualStateManager.GoToState接受Control,带有自定义状态名称的字符串和用于转换的bool标志。
来自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);
}
}

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);

关于wpf - 在xaml中创建自定义VisualState并在CodeBehind中手动进行设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7839235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com