【问题标题】:Metro/Windows Store App: Can't use Foreground as TemplateBinding source in ControlTemplate?Metro/Windows 应用商店应用程序:不能在 ControlTemplate 中使用前台作为 TemplateBinding 源?
【发布时间】:2012-09-16 02:30:15
【问题描述】:

我想使用我的控件的 Foreground 值作为 ControlTemplate 中 VisualState ColorAnimation 的源。

我的模板定义看起来主要像 ToggleButton 的标准模板,带有一些 mods(标记为 >>):

<Style TargetType="ToggleButton>
  <Setter .../>
  ...
  <<< <Setter Property="Foreground" Value="#FF000000"/> >>>
  ...
  <Setter .../>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ToggleButton">
        <Grid>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState .../>
              <VisualState x:Name="PointerOver">
                <Storyboard>
                  <ColorAnimation Duration="0" Storyboard.Target="BackgroundGradient" Storybord.TargetProperty="(Rectangel.Fill).(GradientBrush.GradientStop)[1].(GradientStopColor)" <<< To="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" >>> />
                </Storyboard>
              </VisualState>
      ...
    ...
  ...
</Style>
...
...
<ToggleButton <<< Foreground="#FFFF0000" >>> ...../>

所以我希望看到动画使用设置的前景色 (#FFFF0000) 作为鼠标悬停动画的一部分,但它什么也没做。 当我在动画定义中写 To="#FFFF0000" 时,我得到了预期的结果,但我想保持动画颜色动态并且为我的应用程序中的每个 ToggleButton 不同。

知道如何解决这个问题吗?

请!

编辑:在尝试通过向 ContentPresenter 添加一个带有 LinearGradientBrush 的新 Rectangle 来实现与上述类似的效果之后,其中一个 GradientStop 应该绑定到 {TemplateBinding Foreground},我现在收到一个错误,可能会启发我的问题的原因“‘Windows.UI.xaml.DependencyProperty’类型的对象无法转换为‘System.Windows.DependencyProperty’类型。” 看起来 {TemplateBinding ...} 产生了错误类型的 DependencyProperty 或 GradientStop 预计 Windows 应用商店应用程序中的类型错误。 然而!有没有办法通过 XAML 中的显式类型转换或任何其他解决方法来克服这个问题?

谢谢

【问题讨论】:

    标签: microsoft-metro controltemplate foreground templatebinding coloranimation


    【解决方案1】:

    所以,我终于自己解决了。

    收到错误消息后(请参阅原始帖子的“编辑”),我尝试制作合适的转换器,现在我可以踢自己了,因为没有看到明显的内容!

    这与 WinRT 或 TemplateBinding 或 Windows.UI.Xaml 与 System-Windows 对象之间的不兼容无关。

    我只是没有意识到 Foreground 是 Brush 类型(在我的例子中是 SolidColorBrush),而 GradientStopColor 需要 Color!

    一旦我创建了 BrushToColorConverter 并将其与 RelativeSource 绑定结合使用(因为 TemplateBinding 不允许转换器),它就可以工作了。

      XAML:
    <Page.Resources>
      <local:BrushToColorConverter x:Key="Brush2Color" DefaultOpacity="1.0"/>
    </Page.Resources>
    ...
    <ColorAnimation Duration="0" 
                    Storyboard.TargetName="BackgroundGradient"
                    Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" 
                    To="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
                         Path=Foreground, 
                         Converter={StaticResource Brush2Color}, 
                         ConverterParameter=0.5}"
    />
    
    
    CodeBehind:
    public class BrushToColorConverter : IValueConverter
    {
      private double defaultOpacity = 1;
      public double DefaultOpacity
      {
        set { defaultOpacity = value; }
      }
    
      public object Convert(object value, Type targetType, object parameter, string culture)
      {
        SolidColorBrush brush = value as SolidColorBrush;
        double opacity;
    
        if (!Double.TryParse((string)parameter, out opacity))
          opacity = defaultOpacity;
    
        if (brush == null)
          return Colors.Transparent;
        else
          return Color.FromArgb((byte)(255.0 * brush.Opacity * opacity), 
                                brush.Color.R, 
                                brush.Color.G, 
                                brush.Color.B
                               );
      }
    
      public object ConvertBack(object value, Type targetType, object parameter, string culture)
      {
        return null;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 2012-11-22
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多