Running storyboard animations in windows 8 datatemplate - windows

I'm trying to create a to do list in Windows 8 XAML. when I complete an item, I want a storyboard to erase the completed items. How can I get a storyboard to run on items inside a datatemplate? when I run the storyboard from C#, I get an error that says: WinRT information: Cannot resolve TargetName rectangleToDelete.
My storyboard looks like this:
<Page.Resources>
<Storyboard x:Name="AnimateDelete">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Storyboard.TargetName="rectangleToDelete">
<EasingDoubleKeyFrame KeyTime="0" Value="-0.333"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="-327.5"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="rectangleToDelete">
<EasingDoubleKeyFrame KeyTime="0" Value="0.333"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="-163.25"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Page.Resources>
My Listview template looks like this:
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel Orientation="Horizontal"
Margin="0,0,-393,-15">
<CheckBox x:Name="CheckBoxComplete"
IsChecked="{Binding Complete, Mode=TwoWay}"
Checked="CheckBoxComplete_Checked"
Content="{Binding Text}"
Margin="10,5"
VerticalAlignment="Center"
MinWidth="300" />
<Button x:Name="ButtonDelete"
Click="ButtonDelete_Click"
Content="Delete" />
</StackPanel>
<Rectangle x:Name="rectangle"
Fill="#FFF4F4F5"
Stroke="Black"
StrokeThickness="0"
RenderTransformOrigin="0.5,0.5"
Margin="319,2,-300,-14"
Height="34"
VerticalAlignment="Top">
<Rectangle.RenderTransform>
<CompositeTransform ScaleX="-1" />
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
My codebehind looks like this:
Button btn = (Button)sender;
TodoItem item = btn.DataContext as TodoItem;
StackPanel stk = (StackPanel)btn.Parent;
Grid grd = (Grid)stk.Parent;
Rectangle rect = (Rectangle)grd.Children[1];
rect.Name = "rectangleToDelete";
AnimateDelete.Begin();
as you can see, I've tried finding the rectangle and giving it the correct name, but I get the same error.
any help would be appreciated!
thanks, tom

A DataTemplate has a different namescope than anything outside. You can put the contents of that DataTemplate and the Storyboard in their own UserControl to get it to work.

Related

Windows Phone 8.1: Animation using Storyboard

I am developing an application for windows phone 8.1 runtime i.e. store application. here I am applying Storyboard animation to stackpanel,
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="StkPnl">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.2"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="StkPnl">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.2"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
</DoubleAnimationUsingKeyFrames>
Here I am scaling it like:
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.2"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
Means from 0.2 to 1 and not from 0 to 1.
My question is: If I Scale object(here stackpanel) from 0.2 to 1 it looks like blurred till animation finishes. If I do it from 0 to 1 then it works great.
UPDATE 1:
<StackPanel x:Name="StkPnlName" Visibility="Collapsed" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,512,0,0" RenderTransformOrigin="0.5,0.5">
<StackPanel.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<TranslateTransform/>
</TransformGroup>
</StackPanel.RenderTransform>
<Grid>
<Ellipse Height="38" Width="38" Fill="Blue"/>
<FontIcon Glyph="&#xcode" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" Foreground="White" FontFamily="{StaticResource Resource}"/>
</Grid>
<TextBlock Text="Text" Foreground="Black" MaxHeight="38" FontSize="14" TextAlignment="Center" Width="74" TextTrimming="WordEllipsis" TextWrapping="Wrap" Style="{StaticResource Resource}" FontWeight="SemiBold"/>
</StackPanel>
What I am missing? Anyone came across this situation please suggest.
Thanks.

Lying exceptions in Blend for Visual Studio

Every once in a while I get compiler exceptions in Blend for Visual Studio. Everything is compiling and working wonderfully, then suddenly BAM! I get compiler errors telling me that objects don't exist in a specific namespace WHEN THEY DO. I have to play with cleaning and rebuilding and closing and reopening Blend for a bout an hour before they go away. My libraries are all up to date and everything is otherwise fine. Does no one else have this happen to them? Please, I want it to stop it is an enormous time killer.
I get the compiler errors in Visual Studio as well.
Thanks.
Updates
For instance,
Error 5 The property 'DesignWidth' does not exist in XML namespace 'http://schemas.microsoft.com/expression/blend/2008'. Line 15 Position 5. C:\MyPath\MyControl.xaml 15 5 EasyBuilder
As far as I know, DesignWidth is does very much exist in that namespace.
The XAML (Not that it will help you any)
<UserControl x:Name="UserControl" x:Class="EasyBuilder.MainControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ignore="http://www.ignore.com"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:surface="http://schemas.microsoft.com/surface/2008"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:gif="http://wpfanimatedgif.codeplex.com"
xmlns:local="clr-namespace:EasyBuilder"
xmlns:res="clr-namespace:EasyBuilder.Infrastructure.Resources"
xmlns:system="clr-namespace:System;assembly=mscorlib"
d:DesignWidth="640" d:DesignHeight="480">
<UserControl.Resources>
<res:MainViewStrings x:Key="LocalStrings" />
</UserControl.Resources>
<UserControl.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" Background="{DynamicResource PageBackgroundBrush}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="OpenSelectionStateGroup">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.15">
<VisualTransition.GeneratedEasingFunction>
<CircleEase EasingMode="EaseOut"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="SelectionClosedState"/>
<VisualState x:Name="SelectionOpenedState">
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsHitTestVisible)" Storyboard.TargetName="OpenFileSelection">
<DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
</BooleanAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="CloseFileSelection">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsHitTestVisible)" Storyboard.TargetName="CloseFileSelection">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="OpenFileSelection">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="FileSelectionShadow">
<EasingDoubleKeyFrame KeyTime="0" Value="12.5"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="FileSelection">
<EasingDoubleKeyFrame KeyTime="0" Value="450"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="FileSelectionControl">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Frame x:Name="ContentFrame" Margin="0,0,100,0" Grid.ColumnSpan="2" NavigationUIVisibility="Hidden"/>
<Grid x:Name="FileSelection" Grid.Column="1" Width="100" Background="{DynamicResource AsideSectionBackgroundBrush}">
<Border x:Name="FileSelectionShadow" Width="12.5" HorizontalAlignment="Left">
<Border.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#3F000000" Offset="0"/>
<GradientStop Color="#00000000" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Button x:Name="OpenFileSelection" Template="{DynamicResource InvisibleButtonTemplate}" HorizontalContentAlignment="Right" Panel.ZIndex="1" Margin="0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:GoToStateAction StateName="SelectionOpenedState"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBlock TextWrapping="Wrap" Text="{Binding OpenActionText, Mode=OneWay, Source={StaticResource LocalStrings}}" Margin="0,0,23,0" Style="{DynamicResource AsideSectionLabel}"/>
</Button>
<local:FileSelectionView x:Name="FileSelectionControl" d:LayoutOverrides="Width" Opacity="0" DataContext="{Binding Files, Mode=OneWay}"/>
</Grid>
<Button x:Name="CloseFileSelection" Template="{DynamicResource InvisibleButtonTemplate}" IsHitTestVisible="False" Opacity="0" Margin="0" d:IsHidden="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<ei:GoToStateAction StateName="SelectionClosedState"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Border x:Name="UserMessage" Background="#BF000000" Visibility="{Binding UserMessenger.IsShowingMessage, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}" Grid.ColumnSpan="2">
<Border VerticalAlignment="Center" Style="{DynamicResource UserMessageStyle}" Background="#FFE2E2E2" BorderBrush="{x:Null}" BorderThickness="0" MaxWidth="800" HorizontalAlignment="Center" MinWidth="400">
<Border.Effect>
<DropShadowEffect Opacity="0.5" ShadowDepth="10"/>
</Border.Effect>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Text="{Binding UserMessenger.MessageText, Mode=OneWay}" Margin="0,0,0,10" HorizontalAlignment="Center" MinHeight="58" Style="{DynamicResource TextBlockBase}"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
<Border x:Name="OKAction" Style="{DynamicResource ButtonBorderStyle}" Margin="15,0,0,0" Height="50" VerticalAlignment="Top" HorizontalAlignment="Right" Visibility="{Binding UserMessenger.ShowOkButton, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}">
<Border.Effect>
<DropShadowEffect Opacity="0.33"/>
</Border.Effect>
<Button Template="{DynamicResource FlatButtonTemplate}" Command="{Binding UserMessenger.CloseCommand, Mode=OneWay}" Margin="0">
<Button.CommandParameter>
<system:String>OK</system:String>
</Button.CommandParameter>
<Image Height="44" Source="Skins/Images/OK.png"/>
</Button>
</Border>
<Border x:Name="CancelAction" Style="{DynamicResource ButtonBorderStyle}" Margin="15,0,0,0" Height="50" VerticalAlignment="Top" HorizontalAlignment="Right" Visibility="{Binding UserMessenger.ShowCancelButton, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}">
<Border.Effect>
<DropShadowEffect Opacity="0.33"/>
</Border.Effect>
<Button Template="{DynamicResource FlatButtonTemplate}" Command="{Binding UserMessenger.CloseCommand, Mode=OneWay}" Margin="0">
<Button.CommandParameter>Cancel</Button.CommandParameter>
<Image Height="44" Source="Skins/Images/Cancel.png"/>
</Button>
</Border>
</StackPanel>
</Grid>
</Border>
</Border>
</Grid>
</UserControl>
You need to mark the blend design time namespace as Ignorable or switching between blend and visual studio will not work.
See Getting an error switching back and forth between Blend and VS
Make sure the following is in your XAML namespace declarations
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"

Storyboard can't be executed twice

I tried to use an animation in my app and I'm using only two pages to test the animation.
When the app starts for the first time, I want to animate my application title with a sliding effect. The title should come from outside the page.
I used the following code:
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle"
Text="{Binding LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
Style="{StaticResource PhoneTextNormalStyle}" RenderTransformOrigin="0.5,0.5" >
<TextBlock.RenderTransform>
<CompositeTransform x:Name="ApplicationTitleTransIn" TranslateX="-200"/>
</TextBlock.RenderTransform>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard BeginTime="00:00:0.5">
<DoubleAnimation Duration="00:00:0.7"
Storyboard.TargetName="ApplicationTitleTransIn"
Storyboard.TargetProperty="TranslateX"
From="-200" To="0">
<DoubleAnimation.EasingFunction>
<BackEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</StackPanel>
And it works pretty well.
When I click a button, my application title should move to the right side of my page and then the second page should be displayed.
I created the following storyboard:
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="ApplicationTitleTransOut">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="ApplicationTitle">
<EasingDoubleKeyFrame KeyTime="0" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase EasingMode="EaseIn"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="600">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase EasingMode="EaseIn"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</phone:PhoneApplicationPage.Resources>
In the code behind, the storyboard is executed as follow:
ApplicationTitleTransOut.Completed += delegate
{
ApplicationTitleTransOut.Stop();
var vm = DataContext as MainViewModel;
if (vm != null)
{
vm.OpenPageCommand.Execute(listBox.SelectedItem as TileItem);
}
};
ApplicationTitleTransOut.Begin();
The text will move to the right side of my page and then I'll navigate to the second page.
Until now everything works.
But when I press the back button (on the phone) I've an exception.
ExceptionObject = {System.InvalidOperationException: Cannot resolve TargetName ApplicationTitleTransIn.}
Did I miss something? Is this the right way to achieve this animation?
Thank you for your help.
In your situation, I would structure the XAML layout differently. First of all, it does not seem that you need CompositeTransform, but just TranslateTransform. In this case, use this snippet for RenderTransform:
<TextBlock.RenderTransform>
<TranslateTransform x:Name="ApplicationTitleTransIn" X="-200"/>
</TextBlock.RenderTransform>
Now, when you are binding the DoubleAnimation to it, use relative property conventions:
<DoubleAnimation Duration="00:00:0.7"
Storyboard.TargetName="ApplicationTitle"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
From="-200" To="0">
Same applies to your DoubleAnimationUsingKeyFrames:
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)" Storyboard.TargetName="ApplicationTitle">
Works like a charm.

Windows Phone - Marquee With Template

In windows phone i could easily marquee a textblock. But is there any solution to marquee list of items for which i can define template and bind it to a list of items.
Thanks
Gokoulane Ravi
Although it's not a WP style...
Add storyboard to page resources:
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="Scroll" RepeatBehavior="Forever">
<DoubleAnimation From="480" To="-480" Storyboard.TargetName="translate" Storyboard.TargetProperty="X" Duration="0:0:5" />
</Storyboard>
</phone:PhoneApplicationPage.Resources>
Add ScrollViewer, add StackPanel inside and TextBlock inside:
<ScrollViewer x:Name="LongScrollViewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Hidden" Margin="0,212,0,339" IsEnabled="False" >
<StackPanel Margin="0" Height="58">
<TextBlock x:Name="LongTextBlock" Text="Very long, real long, it's a long text." Margin="0" Style="{StaticResource PhoneTextLargeStyle}" VerticalAlignment="Top"
HorizontalAlignment="Center" TextAlignment="Center" TextTrimming="None" TextWrapping="NoWrap">
<TextBlock.RenderTransform>
<TranslateTransform x:Name="translate" />
</TextBlock.RenderTransform>
</TextBlock>
</StackPanel>
</ScrollViewer>
In page's Loaded method make sure TextBlock's text is long enough for scrolling:
Size size = new Size(double.PositiveInfinity, double.PositiveInfinity);
this.LongTextBlock.Measure(size);
size = this.LongTextBlock.DesiredSize;
if (size.Width > this.ActualWidth)
{
this.Scroll.Begin();
}
You can do that. However you'll need to write custom XAML to achieve this functionality. Blend can help you over here to create that custom animation and run as marquee.

Windows 7 Phone button with multiple textblocks

I have created a button in Blend by editing it's style. I added multiple text blocks with the intention of displaying data to the user in real time. However, I don't know how to interface with those text blocks in my code behind.
My Style XAML is this:
<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="ButtonBackground" BorderThickness="{TemplateBinding BorderThickness}" Background="{StaticResource PhoneAccentBrush}" CornerRadius="0" Margin="8,12,12,12">
<TextBlock Margin="121,5,98,0" TextWrapping="Wrap" Text="Current Program:" Height="36" VerticalAlignment="Top"/>
</Border>
<TextBlock Margin="92,68,80,81" TextWrapping="Wrap" Text="" RenderTransformOrigin="0.265,0.51" HorizontalAlignment="Center" Width="271" x:Name="programName"/>
<TextBlock Height="32" Margin="21,0,16,12" TextWrapping="Wrap" Text="Date:" VerticalAlignment="Bottom" x:Name="CurrentDate"/>
</Grid>
</ControlTemplate>
My code to display the button is this:
<Grid x:Name="middleRow" Grid.Row="2">
<Button Content="Button" Margin="8,8,0,8" Style="{StaticResource ButtonCenter}" x:Name="Current" Click="Current_Click" d:LayoutOverrides="GridBox" />
</Grid>
In my code behind after the InitializeComponent(); I would like to change the ProgramName text block and the CurrentDate text block.
I'm thinking that I might have to create a control to do this but I'm not sure. My attempts at doing so failed (misc. errors). Can I access these text blocks in code? Please let me know.
UPDATE:
I wound up doing it like this:
<Button Margin="8,8,0,8" x:Name="Current" Click="Current_Click">
<Button.Content>
<StackPanel>
<TextBlock x:Name="ProgramName" Text="program name" HorizontalAlignment="Center" />
<TextBlock x:Name="CurrentDate" Text="current date" HorizontalAlignment="Center" />
</StackPanel>
</Button.Content>
</Button>
I applied my styles from the template in Blend and it appears to be working now.
You can't reference controls in a style by name as there could be multiple copies of them on a page.
If you made your button into a custom control you could make the text for the ProgramTitle and CurrentDate properties (which woudl be very easy to set).
Alternatively you could use databinding to set these values.

Resources