[RESOLVED] Sliding objects with code
I am having a slight problem sliding controls on a form with C# code. So far I found this article http://vbcity.com/blogs/xtab/archive...on-in-wpf.aspx on how to do it with XAML, but I need to do it with C# because items will only slide to places depending on what item is selected in a textbox.
I have my controls setup like this:
http://ben-thomson.co.uk/xeN.png
On the left is how it should look with item one selected, and on the right is how it should look with item 2 selected. With the textbox gone the height of the form will also reduce as well and when it comes back (item 1) it will increase.
The code I tried so far is this, but it doesn't seem to work so I must be doing something wrong but don't know what as I am fairly new to WPF.
Code:
Storyboard storyboard = new Storyboard();
TimeSpan duration = new TimeSpan(0, 0, 3);
DoubleAnimation animation = new DoubleAnimation();
//animation.From = 0.0;
//animation.To = 1.0;
animation.By = 100;
animation.Duration = new Duration(duration);
Storyboard.SetTargetName(animation, btnTest.Name);
Storyboard.SetTargetProperty(animation, new PropertyPath(TranslateTransform.XProperty));
storyboard.Children.Add(animation);
storyboard.Begin(this);
Re: Sliding objects with code
If you are not bothered about animating it and simply want the controls below (ie the button) to move up when the textbox is made invisible then you can simply set the textbox's Visibility property to Collapsed rather than Hidden.
However, I'll assume you do want to animate it, so here's an example of how I would do it (this is to slide the button up to just beneath the combobox) :
Code:
Textbox1.Visibility = Windows.Visibility.Hidden
Dim Anim As New Animation.ThicknessAnimation
Anim.To = New Thickness(0, -15, 0, 0)
Anim.Duration = New Duration(TimeSpan.FromSeconds(1))
Button1.BeginAnimation(FrameworkElement.MarginProperty, Anim)
and here's a complete example:
Code:
Class Window1
Private Sub ComboBox1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ComboBox1.SelectionChanged
If ComboBox1.SelectedIndex = 0 Then
SlideDown()
ElseIf ComboBox1.SelectedIndex = 1 Then
SlideUp()
End If
End Sub
Private Sub SlideUp()
Textbox1.Visibility = Windows.Visibility.Hidden
Dim Anim As New Animation.ThicknessAnimation
Anim.To = New Thickness(0, -15, 0, 0)
Anim.Duration = New Duration(TimeSpan.FromSeconds(1))
Button1.BeginAnimation(FrameworkElement.MarginProperty, Anim)
End Sub
Private Sub SlideDown()
Dim Anim As New Animation.ThicknessAnimation
Anim.To = New Thickness(0, 0, 0, 0)
Anim.Duration = New Duration(TimeSpan.FromSeconds(1))
Button1.BeginAnimation(FrameworkElement.MarginProperty, Anim)
Textbox1.Visibility = Windows.Visibility.Visible
End Sub
End Class
Re: Sliding objects with code
Re: [RESOLVED] Sliding objects with code
Re: [RESOLVED] Sliding objects with code
Won't let me add rep to you, says I need to share it around more before giving you more :( lol
Re: [RESOLVED] Sliding objects with code
lol thats ok, its the thought that counts :D