【问题标题】:Wpf color animation bindingwpf彩色动画绑定
【发布时间】:2013-04-06 16:25:16
【问题描述】:

我偶然发现了一个非常烦人的问题,这似乎是一件微不足道的事情,但我无法找到解决方案。我想要做的是将 ColorAnimation 的 To 属性绑定到名为 MouseOverColor 的 DependencyProperty。这就是我的风格:

<Style TargetType="{x:Type local:Button}">
  <Setter Property="Background" Value="{StaticResource SolidBlueDark}" />
  <Setter Property="BorderBrush" Value="{x:Null}" />
  <Setter Property="BorderThickness" Value="0" />
  <Setter Property="FocusVisualStyle" Value="{x:Null}" />
  <Setter Property="Foreground" Value="White" />
  <Setter Property="HorizontalContentAlignment" Value="Center" />
  <Setter Property="MouseOverColor" Value="Cyan" />
  <Setter Property="Padding" Value="1" />
  <Setter Property="VerticalContentAlignment" Value="Center" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:Button}">
        <Grid>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup Name="CommonStates">
              <VisualStateGroup.Transitions>
                <VisualTransition GeneratedDuration="0:0:0.4" />
              </VisualStateGroup.Transitions>
              <VisualState Name="Normal" />
              <VisualState Name="MouseOver">
                <Storyboard>
                  <ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.Background).Color" To="{TemplateBinding MouseOverColor}" />
                </Storyboard>
              </VisualState>
              <VisualState Name="Pressed" />
              <VisualState Name="Disabled" />
            </VisualStateGroup>
            <VisualStateGroup Name="FocusStates">
              <VisualState Name="Unfocused" />
              <VisualState Name="Focused" />
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Border 
            Name="Border" 
            Background="{TemplateBinding Background}" 
            BorderBrush="{TemplateBinding BorderBrush}" 
            BorderThickness="{TemplateBinding BorderThickness}" 
            Height="{TemplateBinding Height}" 
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
            Width="{TemplateBinding Width}" />
          <ContentPresenter 
            TextBlock.Foreground="{TemplateBinding Foreground}" 
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
            Margin="{TemplateBinding Padding}" 
            RecognizesAccessKey="True" 
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

什么都没有发生,按钮的颜色保持不变。如果我将 ColorAnimation 线更改为以下内容,它会更改颜色:

<ColorAnimation 
  Storyboard.TargetName="Border"
  Storyboard.TargetProperty="(Border.Background).Color"
  To="Cyan" />

但是我需要 To 颜色由 MouseOverColor 属性设置,而不是硬编码,我该如何实现呢?我尝试了各种绑定,但结果总是一样。

提前致谢。

编辑:如果我使用静态资源,它也可以工作,但这不是我想要的。真的没有办法做我想做的事吗?为我希望能够使用的每种“鼠标悬停”颜色创建新样式并对其进行硬编码是没有任何意义的。

【问题讨论】:

  • 你试过To="{Binding Path=MouseOverColor,RelativeSource={RelativeSource Mode=TemplatedParent}}"吗?
  • 感谢您的回复,但颜色仍然没有变化。
  • 好吧,这只是一个猜测。 TemplateBinding 并不总是足够的。
  • 尝试更多dynamic - {Binding RelativeSource={RelativeSource TemplatedParent}, Path=YourColorProperty} 如果您想绑定它自己的属性。请参阅我在stackoverflow.com/questions/15435385/… 发布的这篇文章。告诉我

标签: wpf binding coloranimation


【解决方案1】:

根据this MSDN forum thread 中的一位微软作者,WPF 不支持您尝试执行的操作。动画值必须被冻结,因此它们不能是动态的或绑定的。我只是偶然发现了同样的限制。如果您尝试这样做,则不会引发任何错误,它只是没有设置任何颜色。

【讨论】:

    【解决方案2】:

    不确定这是否适合您的情况...

    如果 MouseOverColor 是您自己的自定义属性 - 请尝试其他方式让 Templated Binding 工作。

    {Binding RelativeSource={RelativeSource TemplatedParent}, Path=MouseOverColor}  
    

    我在这里做了一些更详细的解释

    Image.Height TemplateBinding does not work

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-20
      • 2011-07-15
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      相关资源
      最近更新 更多