【问题标题】:Bind EasingColorKeyFrame color Value绑定 EasingColorKeyFrame 颜色值
【发布时间】:2012-12-14 05:08:26
【问题描述】:

我有以下 Windows Phone 代码,用于将 Rectangle 的颜色从黑色更改为红色。

<Grid x:Name="LayoutRoot">
  <phone:Pivot x:Name="MyPivot" >
    <phone:PivotItem x:Name="MyPivotItem">
      <Canvas x:Name="MyCanvas">
        <Canvas.Resources>
          <Storyboard x:Name="MyStoryboard">
            <ColorAnimationUsingKeyFrames
              Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)"
              AutoReverse="True">
                <EasingColorKeyFrame KeyTime="00:00:0" Value="Black" />
                <EasingColorKeyFrame KeyTime="00:00:0.5" Value="Red" />
            </ColorAnimationUsingKeyFrames>
          </Storyboard>
        </Canvas.Resources>
      </Canvas>
    </phone:PivotItem>
  </phone:Pivot>
</Grid>

我想做的是让它更加动态,以便我可以从代码隐藏页面更改EasingColorKeyFrames 中使用的颜色。是否可以将我的EasingColorKeyFrames 的Value 绑定到我的代码隐藏页面中的属性?

【问题讨论】:

  • Arent you trying to bind to properties of Grid named 'LayoutRoot' that doesnt 有你的颜色属性吗?在文章中作者绑定到 UserControl 本身,而不是 Grid。
  • 啊,你是对的。我想我误解了第一次但再次阅读它似乎更清楚,我会从问题中删除它。你知道我可以从我的代码隐藏页面绑定属性的方法吗?
  • @AntSlay 对这个问题的回答与我之前尝试的类似,但使用的是页面名称而不是 LayoutRoot:stackoverflow.com/questions/5130937/…。今晚我会试一试!

标签: silverlight binding storyboard windows-phone windows-phone-8


【解决方案1】:

如果您只是想从代码隐藏页面更改颜色,那么只需给您的框架一个 x:Name 并在后面的代码中设置它们的值。

...
<EasingColorKeyFrame KeyTime="00:00:0" x:Name="start" Value="Black" />
<EasingColorKeyFrame KeyTime="00:00:0.5" x:Name="end" Value="Red" />
...

代码隐藏:

private void ChangeColors()
{
  start.Value = Colors.Green;
  end.Value = Colors.Blue;
}

【讨论】:

  • 哇哦。我不知道我是怎么想到的,但谢谢。这绝对是最简单的解决方案。今晚我会试试这个!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-14
  • 2018-01-11
  • 2011-01-09
  • 1970-01-01
  • 2016-08-03
  • 1970-01-01
相关资源
最近更新 更多