locked
Animating colors of ContentPresenter bugged? RRS feed

  • Question

  • using the Storyboard we are trying to have an animation to change the foreground color of the ContentPresenter inside the template of a control. However this gives us an error:

    An exception of type 'System.Exception' occurred in System.Windows.dll but was not handled in user code

    Additional information: Error HRESULT E_FAIL has been returned from a call to a COM component.
     

    this works though with Textblock.

    Monday, March 17, 2008 5:38 AM

All replies

  • Are they both in the same XAML file? (e.g. the same resources)?

    Monday, March 17, 2008 5:31 PM
  • yup. both control and the template definition are in the same xaml.

    Tuesday, March 18, 2008 2:49 AM
  • Any chance we can see our XAML and code?

    Tuesday, March 18, 2008 12:05 PM
  • <RadioButton x:Class="Controls.TabButton"
        xmlns="http://schemas.microsoft.com/client/2007"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="197" Height="43">
     <RadioButton.Template>
      <ControlTemplate TargetType="RadioButton">
       <Grid x:Name="RootElement">
       <Grid.Resources>
                    <Storyboard x:Name="MouseOver Unchecked State">
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="text" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" BeginTime="00:00:00">
             <SplineColorKeyFrame x:Name="overFrame" KeyTime="00:00:00.3000000" Value="Blue"/>
            </ColorAnimationUsingKeyFrames>
           </Storyboard>
                    <Storyboard x:Name="Pressed Checked State">
                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="text" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" BeginTime="00:00:00" Duration="00:00:00.0010000">

                        <!--<ColorAnimationUsingKeyFrames Storyboard.TargetName="text" Storyboard.TargetProperty="(ContentPresenter.Foreground).(SolidColorBrush.Color)" BeginTime="00:00:00" Duration="00:00:00.0010000"> -->
                         <SplineColorKeyFrame x:Name="pressedCheckedFrame" KeyTime="00:00:00" Value="Red"/>
                        </ColorAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00">
                         <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Name="Normal State">
         <ColorAnimationUsingKeyFrames Storyboard.TargetName="text" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" BeginTime="00:00:00">

         <!--<ColorAnimationUsingKeyFrames Storyboard.TargetName="text" Storyboard.TargetProperty="(ContentPresenter.Foreground).(SolidColorBrush.Color)" BeginTime="00:00:00">-->
             <SplineColorKeyFrame x:Name="normalFrame" KeyTime="00:00:00.3000000" Value="Green"/>
            </ColorAnimationUsingKeyFrames>
         <DoubleAnimationUsingKeyFrames Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00">
          <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
         </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Name="Checked State">
                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="text" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" BeginTime="00:00:00" Duration="00:00:00.0010000">

    <!--                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="text" Storyboard.TargetProperty="(ContentPresenter.Foreground).(SolidColorBrush.Color)" BeginTime="00:00:00" Duration="00:00:00.0010000"> -->
                         <SplineColorKeyFrame x:Name="checkedFrame" KeyTime="00:00:00" Value="Red"/>
                        </ColorAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00">
                         <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </Grid.Resources>
           <Image IsHitTestVisible="False" HorizontalAlignment="Left" Margin="0,0,0,0" Width="197" Source="Images/tab.png" Stretch="Fill" VerticalAlignment="Top" Height="43" x:Name="image" Opacity="0"/>
                    <TextBlock x:Name="text" Text="RadioButton" HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand"
    FontFamily="{TemplateBinding FontFamily}"
    FontSize="{TemplateBinding FontSize}"
    FontStretch="{TemplateBinding FontStretch}"
    FontStyle="{TemplateBinding FontStyle}"
    FontWeight="{TemplateBinding FontWeight}"
    Foreground="{TemplateBinding Foreground}"
                            TextDecorations="{TemplateBinding TextDecorations}"
    TextWrapping="{TemplateBinding TextWrapping}">
                    </TextBlock>

    <!--                <ContentPresenter x:Name="text" HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand"

    Content="{TemplateBinding Content}"
    FontFamily="{TemplateBinding FontFamily}"
    FontSize="{TemplateBinding FontSize}"
    FontStretch="{TemplateBinding FontStretch}"
    FontStyle="{TemplateBinding FontStyle}"
    FontWeight="{TemplateBinding FontWeight}"
    Foreground="{TemplateBinding Foreground}"
                            TextDecorations="{TemplateBinding TextDecorations}"
    TextWrapping="{TemplateBinding TextWrapping}">
                    </TextBlock>-->

          </Grid>
    </ControlTemplate>
     </RadioButton.Template>
    </RadioButton>

    namespace Controls

    {

    using System.Windows.Controls;

    using System.Windows.Media;

    using System.Windows.Media.Animation;

    public partial class TabButton : RadioButton

    {

    public TabButton()

    {

    InitializeComponent();

    }

    }

    }

     this works fine with TextBlock. The commented parts are the ones using ContentPresenter and this gives the error.

    Tuesday, March 18, 2008 11:21 PM
  • Hi,

     i have a very similar problem. It appears that the Foreground Property of the ContentPresenter can't be animated. Did you find a solution?

    <Style x:Key="ButtonIconDeleteStyle" TargetType="Button">

    <Setter Property="Template">

    <Setter.Value>

    <ControlTemplate TargetType="Button">

    <Grid x:Name="RootElement">

    <Grid.Resources>

    <Storyboard x:Key="MouseOver State">

    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ico_del" Storyboard.TargetProperty="Fill" Duration="0:0:0" >

    <ObjectAnimationUsingKeyFrames.KeyFrames>

    <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource White}"/>

    </ObjectAnimationUsingKeyFrames.KeyFrames>

    </ObjectAnimationUsingKeyFrames>

    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Foreground" Duration="0:0:0" >

    <ObjectAnimationUsingKeyFrames.KeyFrames>

    <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource White}"/>

    </ObjectAnimationUsingKeyFrames.KeyFrames>

    </ObjectAnimationUsingKeyFrames>

    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderBrush" Duration="0:0:0" >

    <ObjectAnimationUsingKeyFrames.KeyFrames>

    <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource DarkGold}" />

    </ObjectAnimationUsingKeyFrames.KeyFrames>

    </ObjectAnimationUsingKeyFrames>

    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="Background" Duration="0:0:0" >

    <ObjectAnimationUsingKeyFrames.KeyFrames>

    <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource ButtonActiveBackgroundBrush}"/>

    </ObjectAnimationUsingKeyFrames.KeyFrames>

    </ObjectAnimationUsingKeyFrames>

    </Storyboard>

    </Grid.Resources>

    <Border

    x:Name="Border"

    CornerRadius="0,0,7,7"

    BorderThickness="1"

    Background="{StaticResource ButtonBackgroundBrush}"

    BorderBrush="{StaticResource DarkGray}">

    <StackPanel HorizontalAlignment="Center" Width="Auto" Height="24" Orientation="Horizontal"

    VerticalAlignment="Center">

    <Canvas x:Name="remove_minus" Width="30" Height="30" Canvas.Left="0" Canvas.Top="0">

    <Rectangle x:Name="ico_del" Width="22.5" Height="6.5" Canvas.Left="3.15" Canvas.Top="8.75"

    Stretch="Fill" StrokeThickness="0.5" StrokeLineJoin="Round" Stroke="#FF000000"

    Fill="{StaticResource RedGradient}"/>

    </Canvas>

    <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Height="24"

    x:Name="contentPresenter" Margin="0,6,0,0"/>

    </StackPanel>

    </Border>

    </Grid>

    </ControlTemplate>

    </Setter.Value>

    </Setter>

    </Style>

     

    Greets

    Sebastian Dusch

    Wednesday, March 26, 2008 1:57 PM
  • We used TextBlock instead

    Wednesday, March 26, 2008 11:20 PM
  •  I've just run into the same problem. I was trying to animate a ContentPresenter in a button template, I switched to TextBlocks but now the TextBlocks have disappeared - what the hell ?

    Thursday, May 15, 2008 4:14 PM
  • aight, check the fontsize, fontfamily of the button/textblock i think it resets it to zero.

    Friday, May 16, 2008 3:32 AM
  •  No that's not it, apparently the

    <ButtonTemplate> 

        <TextBlock Text="{TemplateBinding Content}"/> 

    </ButtonTemplate> 

     

    Isn't legal.

     

    What I'm trying to do is have 2 "content holders". When I hover the mouse on the button I want one to disappear and the other one to appear by changing their foreground opacities 

    Friday, May 16, 2008 3:45 AM
  • what we did is have a separate control and changed the Text on OnApplyTemplate

    <RadioButton x:Class="TabButton"

    xmlns="http://schemas.microsoft.com/client/2007"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="197" Height="43">

    <RadioButton.Template>

    <ControlTemplate TargetType="RadioButton">

    <Grid x:Name="RootElement">

    <Grid.Resources>

    <Storyboard x:Name="MouseOver Unchecked State">

    <ColorAnimationUsingKeyFrames Storyboard.TargetName="text" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" BeginTime="00:00:00">

    <SplineColorKeyFrame x:Name="overFrame" KeyTime="00:00:00.3000000" Value="Blue"/>

    </ColorAnimationUsingKeyFrames>

    </Storyboard>

    <Storyboard x:Name="Normal State">

    <ColorAnimationUsingKeyFrames Storyboard.TargetName="text" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" BeginTime="00:00:00">

    <SplineColorKeyFrame x:Name="normalFrame" KeyTime="00:00:00.3000000" Value="Green"/>

    </ColorAnimationUsingKeyFrames>

    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00">

    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>

    </DoubleAnimationUsingKeyFrames>

    </Storyboard>

    <Storyboard x:Name="Checked State">

    <ColorAnimationUsingKeyFrames Storyboard.TargetName="text" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" BeginTime="00:00:00" Duration="00:00:00.0010000">

    <SplineColorKeyFrame x:Name="checkedFrame" KeyTime="00:00:00" Value="Red"/>

    </ColorAnimationUsingKeyFrames>

    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00">

    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/>

    </DoubleAnimationUsingKeyFrames>

    </Storyboard>

    </Grid.Resources>

    <Image IsHitTestVisible="False" HorizontalAlignment="Left" Margin="0,0,0,0" Width="197" Source="Images/tab.png" Stretch="Fill" VerticalAlignment="Top" Height="43" x:Name="image" Opacity="0"/>

    <TextBlock x:Name="text" Text="RadioButton" Margin="0,6,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand"

    FontFamily="{TemplateBinding FontFamily}"

    FontSize="{TemplateBinding FontSize}"

    FontStretch="{TemplateBinding FontStretch}"

    FontStyle="{TemplateBinding FontStyle}"

    FontWeight="{TemplateBinding FontWeight}"

    Foreground="{TemplateBinding Foreground}"

    TextDecorations="{TemplateBinding TextDecorations}"

    TextWrapping="{TemplateBinding TextWrapping}">

    </TextBlock>

    </Grid>

    </ControlTemplate>

    </RadioButton.Template>

    </RadioButton>

     

    public partial class TabButton : RadioButton

    {

    private TextBlock text;

    private SplineColorKeyFrame overFrame;

    private SplineColorKeyFrame normalFrame;

    private SplineColorKeyFrame checkedFrame;

    public new object Content { get;set; }

    public SolidColorBrush ForegroundHighlight { get; set; }

    public TabButton()

    {

    InitializeComponent();

    Content =
    string.Empty;

    ForegroundHighlight = new SolidColorBrush();

    ForegroundHighlight.SetValue(SolidColorBrush.ColorProperty, "#FF646464");

    FontSize = 28;

    Foreground =
    new SolidColorBrush();

    Foreground.SetValue(SolidColorBrush.ColorProperty, "#FFC0C0C0");

    FontFamily = new FontFamily("Arial");

    }

    protected override void OnApplyTemplate()

    {

    base.OnApplyTemplate();text = GetTemplateChild("text") as TextBlock;

    text.Text = Content.ToString();

    checkedFrame = GetTemplateChild(
    "checkedFrame") as SplineColorKeyFrame;

    checkedFrame.Value = ForegroundHighlight.Color;

    overFrame = GetTemplateChild(
    "overFrame") as SplineColorKeyFrame;

    overFrame.Value = ForegroundHighlight.Color;

    normalFrame = GetTemplateChild("normalFrame") as SplineColorKeyFrame;

    if (Foreground is SolidColorBrush)normalFrame.Value = ((SolidColorBrush)Foreground).Color;

    }

    }

    Friday, May 16, 2008 4:02 AM
  •  Looks fun, I'll try that. Thanks !

    Friday, May 16, 2008 5:13 AM
  •  Ok, I'm lost, I've created a new UserControl , named it TabButton.xaml and replaced the xaml with the one you gave me, other then that I've replaced the codeBehind. For some reason it doesn't work for me lol.

    Friday, May 16, 2008 6:16 AM