I have two canvases and a textblock with name tbmarquee, on window load it animates from top to bottom, but i want to stop when i over the mose on it, and when i click it goes to the link, how to do it ?
void Window1_Loaded(object sender, RoutedEventArgs e)
{
DoubleAnimation doubleAnimation = new DoubleAnimation();
doubleAnimation.From = -tbmarquee.ActualHeight;
doubleAnimation.To = canMain.ActualHeight;
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10"));
tbmarquee.BeginAnimation(Canvas.TopProperty, doubleAnimation);
}
On mouse over event handler of tbmarquee, call BeginAnimation() again with second argument set to null to stop the animation :
tbmarquee.BeginAnimation(Canvas.TopProperty, null);
Related question : How to stop an animation in C# / WPF?
Related
I want to move a window with a storyboard. But after the storyboard completed, if you move the window with mouse, the property "Window.Left" doesn't change when you move the window. I can set "FillBehavior=FillBehavior.Stop" to solve this problem, but I don't know why.
public void MoveWindow()
{
Storyboard storyboard = new Storyboard();
DoubleAnimation xAnimation = new DoubleAnimation();
Storyboard.SetTarget(xAnimation, this);
Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(Window.Left)"));
xAnimation.From = this.Left;
xAnimation.To = this.Left + 100;
xAnimation.Duration = TimeSpan.FromSeconds(0.2);
storyboard.Children.Add(xAnimation);
storyboard.Begin();
}
private void btn_Move_Click(object sender, RoutedEventArgs e)
{
MoveWindow();
}
Set FillBehavior.Stop to ensure that the animation does not hold the property value.
From the Remarks in the online documentation:
Set an animations FillBehavior property to HoldEnd when you want the animation to hold its value after it reaches the end of its active period. An animation that has reached the end of its active period that has a FillBehavior setting of HoldEnd is said to be in its fill period. When you don't want an animation to hold its value after it reaches the end of its active period, set its FillBehavior property to Stop.
Add a Completed event handler to set the current property after the animation has run.
public void MoveWindow()
{
var xAnimation = new DoubleAnimation
{
To = Left + 100,
Duration = TimeSpan.FromSeconds(0.2),
FillBehavior = FillBehavior.Stop
};
xAnimation.Completed += (s, e) => Left += 100;
BeginAnimation(LeftProperty, xAnimation);
}
I have a WPF Image inside a Border I currently am able to click and drag to pan the image. I want to prevent users from dragging the image off the screen. Perhaps a minimum of 100 pixels should be showing at any border. That is, if a user drags the image all the way to the left, most of the image will disappear, but 100 pixels will still be hanging out toward the left boundary of the border.
Here's my current code for the MouseMove and LeftClick event:
private void img_Box_MouseMove(object sender, MouseEventArgs e)
{
if (!img_Box.IsMouseCaptured) return;
var tt = (TranslateTransform)((TransformGroup)img_Box.RenderTransform).Children.First(tr => tr is TranslateTransform);
Vector v = start - e.GetPosition(img_Border);
tt.X = origin.X - v.X;
tt.Y = origin.Y - v.Y;
}
private void img_Box_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
img_Box.CaptureMouse();
var tt = (TranslateTransform)((TransformGroup)img_Box.RenderTransform).Children.First(tr => tr is TranslateTransform);
start = e.GetPosition(img_Border);
origin = new System.Windows.Point(tt.X, tt.Y);
}
I think I should be able to put a pretty simple condition on that to achieve my desired result, but I can't figure out what the condition should be.
i am trying to rotate an object from code behind. Code below:
Storyboard storyBoard = new Storyboard();
//Transform
RotateTransform rotate = new RotateTransform();
rotate.Angle = 45;
rotate.CenterX = 50;
rotate.CenterY = 20;
RodBorder.RenderTransform = rotate;
DoubleAnimation Anim = new DoubleAnimation();
Anim.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
Anim.SetValue(Storyboard.TargetPropertyProperty, rotate);
Storyboard.SetTargetProperty(Anim, new PropertyPath("RenderTransform.Angle"));
Storyboard.SetTarget(Anim, RodBorder);
storyBoard.Children.Add(Anim);
storyBoard.Begin();
RodBorder is a Border which i want to rotate. The problem here is probably the PropertyPath, because i got an exception there.(System.Windows.PropertyPath cannot be set to type System.Windows.Media.RotateTransform) Any ideas are welcome, thank you for your help.
Try Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Rotation)".
You can always verify syntax by building sample animation in Blend.
I have a wpf window that hosts a control.
The window's height and width are set to SizeToControl.
Now I need to position this Window relative to the position of its parent window.(basically the Top right position).
(So my windows Top = ParentWindow.Top, and Left = ParentWindow.Left + ParentWindow.ActualWidth - control.ActualWidth so that my window is positioned inside the parent window but to its right corner)
So i will need to set the Top and Left of the window. To do this I need the Actual Width of the control that is being hosted inside it....but I can only get this once I actually do,
Window.Show(control,parent)
Is there a way to get around this problem? How do I get the actual rendered width of the control before it is actually shown?
Thanks!
Have you tried this approach?
public partial class ShellWindow : Window
{
public ShellWindow(IShellPresentationModel model)
{
InitializeComponent();
this.Loaded += ShellWindow_Loaded;
}
void ShellWindow_Loaded(object sender, RoutedEventArgs e)
{
var innerWindow = new InnerWindow();
innerWindow.Owner = this;
innerWindow.Loaded += InnerWindow_Loaded;
innerWindow.Show();
}
void InnerWindow_Loaded(object sender, RoutedEventArgs e)
{
var w = (InnerWindow)sender;
w.Owner = this;
w.Top = this.Top;
w.Left = this.Left + this.ActualWidth - w.ActualWidth;
}
}
I used this code to animation my window:
winLogin login = new winLogin();
login.Owner = this;
login.Show();
DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = this.Left + ((this.Width - login.Width) / 2);
da.AutoReverse = false;
da.Duration = new Duration(TimeSpan.FromSeconds(0.1));
login.BeginAnimation(Window.LeftProperty, da);
Problem is that whenever i set the Left property of this window(after the animation), it goes crazy.
I used this code to align the child windows to be always on the center but the Left property of the windows on which i used an animation cannot be properly changed.
private void Window_LocationChanged(object sender, EventArgs e)
{
foreach (Window win in this.OwnedWindows)
{
win.Top = this.Top + ((this.Height - win.Height) / 2);
win.Left = this.Left + ((this.Width - win.Width) / 2);
}
}
First of all, when you set an animation you should always remove the potential previous animation of that property:
login.BeginAnimation(Window.LeftProperty, null);
login.BeginAnimation(Window.LeftProperty, da);
If you don't so this you will get a memory leak and probably some other undesired behavior.
Also due to the DependencyProperty precedence you can not set a value on a DependecyProperty that has an active animation, wich is the case in your animation because its FillBehavior is set to HoldEnd (the default). Again you would have to remove the animation first.