对内容的状态更改做出反应(Windows Phone)
本文关键字:Windows Phone 状态 | 更新日期: 2023-09-27 18:28:27
控件的内容是否可能对其父控件的视觉状态更改做出反应?
在下面的示例中,当父控件处于活动状态时,我希望文本块的前景变为红色。但这个代码不起作用:
<local:ExpandKeyButton x:Name="xpand" Height="30">
<TextBlock x:Name="tb">
Test
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="General">
<VisualState x:Name="Idle" />
<VisualState x:Name="Active">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="tb" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</TextBlock>
</local:ExpandKeyButton>
否--视觉状态是控件内部的,因为所属ControlTemplate之外的UI元素不能(直接)参与视觉状态更改。我可以在你的场景中看到一些可能性:
- 如果"tb"TextBlock是"ExpandKeyButton"的通用功能,则将其移动到ControlTemplate中,以允许其参与视觉状态更改
- 或者,对于更松散耦合的方法,向"ExpandKeyButton"控件添加一个属性或一个事件,以允许TextBlock绑定到其状态。例如,您可以添加一个"IsActive"依赖属性,可以在ControlTemplate的VisualStateManager中适当设置该属性,然后TextBlock可以使用类似
Foreground="{Binding ElementName=xpand,Path=IsActive,Converter={StaticResource ActiveToColorConverter}}"
的绑定,在控件处于活动状态时使其变为红色