【问题标题】:React on state change in content (Windows Phone)对内容的状态变化做出反应(Windows Phone)
【发布时间】:2014-10-30 21:17:23
【问题描述】:

控件的内容是否可以对其父级的视觉状态变化做出反应?

在以下示例中,当父控件处于活动状态时,我希望文本块的前景变为红色。但是这段代码不起作用:

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

【问题讨论】:

  • storyboard可以向内定位——也就是说,如果你在local:ExpandKeyButton上设置了VisualStateGroups,它仍然可以修改TextBlock
  • @bdimag 第一部分是真的,但第二部分不是,因为VisualStateGroups 在那里无效。 VisualStateGroups 只能在 ControlTemplate 或 UserControl 的根元素下定义。

标签: c# wpf xaml windows-phone windows-phone-8.1


【解决方案1】:

否 - 视觉状态是控件内部的,在某种意义上,拥有 ControlTemplate 之外的 UI 元素不能(直接)参与视觉状态更改。我可以在您的场景中看到一些可能性:

  1. 如果“tb”TextBlock 是“ExpandKeyButton”的一般功能,则将其移动到 ControlTemplate 内,以使其参与视觉状态更改。
  2. 或者,对于更松散耦合的方法,将属性或事件添加到“ExpandKeyButton”控件,以允许 TextBlock 绑定到其状态。例如,您可以添加一个“IsActive”依赖属性,您可以在 ControlTemplate 的 VisualStateManager 中对其进行适当设置——然后 TextBlock 可以使用Foreground="{Binding ElementName=xpand,Path=IsActive,Converter={StaticResource ActiveToColorConverter}}" 之类的绑定在控件处于活动状态时使其变为红色。

【讨论】:

    猜你喜欢
    • 2020-05-18
    • 2021-06-06
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    相关资源
    最近更新 更多