2014-10-30 37 views
1

控件的内容是否可以对其父项的视觉状态变化做出反应?对内容状态变化的反应(Windows Phone)

在下面的例子中,当父控件处于活动状态时,我希望textblock的前景变为红色。但是这个代码不工作:

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

,故事情节可以向内的目标 - 那就是,如果你设置本地的VisualStateGroups:ExpandKeyButton,它仍然可以修改TextBlock的 – bdimag 2014-10-30 21:23:55

+0

@bdimag第一部分是真实的,但第二个不是,因为'VisualStateGroups'在那里将是无效的。 VisualStateGroups只能在ControlTemplate或UserControl的根元素下定义。 – McGarnagle 2014-10-31 00:47:53

回答

1

否 - 可视状态是内部控制,在这个意义上,拥有控件模板外UI元素不能(直接)参与的视觉状态的变化。我能看到一些可能在您的方案:

  1. 如果“TB”的TextBlock是“ExpandKeyButton”的一般特征,然后将它的控件模板内,要允许它参与的视觉状态的变化。
  2. 或者,对于更松散耦合的方法,请将属性或事件添加到“ExpandKeyButton”控件,以允许TextBlock绑定到其状态。例如,您可以添加一个“IsActive”依赖项属性,您可以在ControlTemplate的VisualStateManager内适当地设置该属性,然后当控件处于活动状态时,TextBlock可以使用类似Foreground="{Binding ElementName=xpand,Path=IsActive,Converter={StaticResource ActiveToColorConverter}}"的绑定。