How can I programatically change the Color of a Rectangle in my Grid?
ColorAnimation myColorAnimation = new ColorAnimation();
myColorAnimation.From = Colors.Red;
myColorAnimation.To = Colors.Blue;
myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
myColorAnimation.AutoReverse = false;
myStoryboard = new Storyboard();
myStoryboard.Children.Add(myColorAnimation);
Storyboard.SetTargetName(myColorAnimation, ?); // What do I put here
Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath //What do I put here?
OK, I found a way to do this:
I create a StoryBoard for each Grid item:
List<Storyboard> _animatableGridRectToGray = new List<Storyboard>();
List<Storyboard> _animatableGridRectToWhite = new List<Storyboard>();
and populate:
private void initAnimatableGridRectangles()
{
int index = 1;
foreach (Rectangle rect in SequenceGrid.Children)
{
SolidColorBrush tempBrush = new SolidColorBrush();
rect.Fill = tempBrush;
string brushName = "Brush" + index;
_gridBrushes.Add(brushName, tempBrush);
this.RegisterName(brushName, tempBrush);
Storyboard tempSBToGray = new Storyboard();
Storyboard tempSBToWhite = new Storyboard();
ColorAnimation tempColAnimToGray = getAnimToGray();
ColorAnimation tempColAnimToWhite = getAnimToWhite();
tempSBToGray.Children.Add(tempColAnimToGray);
tempSBToWhite.Children.Add(tempColAnimToWhite);
Storyboard.SetTargetName(tempColAnimToGray, brushName);
Storyboard.SetTargetName(tempColAnimToWhite, brushName);
Storyboard.SetTargetProperty(tempColAnimToGray, new PropertyPath(SolidColorBrush.ColorProperty));
Storyboard.SetTargetProperty(tempColAnimToWhite, new PropertyPath(SolidColorBrush.ColorProperty));
_animatableGridRectToGray.Add(tempSBToGray);
_animatableGridRectToWhite.Add(tempSBToWhite);
index++;
}
}
private ColorAnimation getAnimToGray()
{
ColorAnimation colAnim = new ColorAnimation();
colAnim.To = Colors.Gray;
colAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500));
colAnim.AutoReverse = false;
return colAnim;
}
private ColorAnimation getAnimToWhite()
{
ColorAnimation colAnim = new ColorAnimation();
colAnim.To = Colors.White;
colAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500));
colAnim.AutoReverse = false;
return colAnim;
}
Then I can animate like so:
_animatableGridRectToGray[index].Begin(this);
Related
hi I wrote allready a working code;
private void baslangic_gridler(UIElement element)
{
DoubleAnimation opaklk = new DoubleAnimation();
opaklk.From = 0;
opaklk.To = 1;
opaklk.Duration = new TimeSpan(0, 0, 0, 0, 300);
Thickness to_margin = (element as Grid).Margin;
Thickness from_margin = to_margin;
from_margin.Left = from_margin.Left - 25;
ThicknessAnimation kaydır = new ThicknessAnimation();
kaydır.From = from_margin;
kaydır.To = to_margin;
kaydır.Duration = new TimeSpan(00,0,0,0,300);
element.BeginAnimation(MarginProperty, kaydır);
element.BeginAnimation(OpacityProperty, opaklk);
}
but I want to use all type of forms element like button or image
Thickness to_margin = (element as Grid).Margin;
what can I use instead of grid for all the elements?
Per MSDN, the Grid class (System.Windows.Controls) inherits it's Margin property from FrameworkElement so you should use that to cover all your bases.
Margin: Gets or sets the outer margin of an element.(Inherited from FrameworkElement.)
Note that a FrameworkElement is a UIElement so you could just take the FrameworkElement as the parameter to your method instead of the UIElement like so:
private void baslangic_gridler(FrameworkElement element)
{
DoubleAnimation opaklk = new DoubleAnimation();
opaklk.From = 0;
opaklk.To = 1;
opaklk.Duration = new TimeSpan(0, 0, 0, 0, 300);
Thickness to_margin = element.Margin;
Thickness from_margin = to_margin;
from_margin.Left = from_margin.Left - 25;
ThicknessAnimation kaydır = new ThicknessAnimation();
kaydır.From = from_margin;
kaydır.To = to_margin;
kaydır.Duration = new TimeSpan(00,0,0,0,300);
element.BeginAnimation(MarginProperty, kaydır);
element.BeginAnimation(OpacityProperty, opaklk);
}
Hi i have a Window>grid>rectangle named as (rect1)
how do i enlarge this using storyboard
Error:Additional information: No applicable name scope exists to resolve the name 'rect1'
private void Window_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Storyboard buttonEnlargeStoryboard = new Storyboard();
DoubleAnimation da = new DoubleAnimation();
da.SetValue(Storyboard.TargetNameProperty, rect1.Name);
da.BeginTime = new TimeSpan(0);
da.Duration = TimeSpan.FromSeconds(1);
buttonEnlargeStoryboard.Children.Add(da);
buttonEnlargeStoryboard.Begin();
}
You should animate width and height properties like this:
DoubleAnimation widthAnimation = new DoubleAnimation
{
From = 0,
To = rect1.ActualWidth*2,
Duration = TimeSpan.FromSeconds(5)
};
DoubleAnimation heightAnimation = new DoubleAnimation
{
From = 0,
To = rect1.ActualHeight*2,
Duration = TimeSpan.FromSeconds(5)
};
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Rectangle.WidthProperty));
Storyboard.SetTarget(widthAnimation, rect1);
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Rectangle.HeightProperty));
Storyboard.SetTarget(heightAnimation, rect1);
Storyboard buttonEnlargeStoryboard = new Storyboard();
buttonEnlargeStoryboard.SpeedRatio = 1;
buttonEnlargeStoryboard.Children.Add(widthAnimation);
buttonEnlargeStoryboard.Children.Add(heightAnimation);
buttonEnlargeStoryboard.Begin();
private void Window_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
DoubleAnimation rightAnimation = new DoubleAnimation();
rightAnimation.Duration = TimeSpan.FromSeconds(2);
rightAnimation.From = 0;
rightAnimation.To = 200;
Storyboard.SetTarget(rightAnimation , rect1);
Storyboard.SetTargetProperty(rightAnimation, new PropertyPath("(0).(1)", new DependencyProperty[]{UIElement.RenderTransformProperty, TranslateTransform.XProperty}));
Storyboard sb = new Storyboard();
sb.AccelerationRatio = 0.8;
sb.Children.Add(rightAnimation);
sb.Begin();
}
I want to accelerate the rect1 to the right.. When i run it nothing happens.
You can use TransformGroup to animate all transform types.
DoubleAnimation rightAnimation = new DoubleAnimation();
rightAnimation.Duration = TimeSpan.FromSeconds(2);
rightAnimation.From = 0;
rightAnimation.To = 200;
TransformGroup transGroup = new TransformGroup();
transGroup.Children.Add(new TranslateTransform());
rect1.RenderTransform = transGroup;
Storyboard.SetTarget(rightAnimation, rect1);
Storyboard.SetTargetProperty(rightAnimation, new PropertyPath("RenderTransform.Children[0].X"));
Storyboard sb = new Storyboard();
sb.AccelerationRatio = 0.8;
sb.Children.Add(rightAnimation);
sb.Begin();
How to animate a DropShadowEffect's color that has been already applied to an element with XAML without the need to re-apply a new DropShadowEffect?
I tried this:
private void test()
{
DropShadowEffect DS_Moon = (DropShadowEffect)Application.Current.Resources["DS_Moon"];
ColorAnimation DS_Moonlight = new ColorAnimation();
DS_Moonlight.From = new Color()
{
A = (byte)1,
R = (byte)0,
G = (byte)0,
B = (byte)0
};
DS_Moonlight.To = new Color()
{
A = (byte)1,
R = (byte)255,
G = (byte)255,
B = (byte)255
};
DS_Moon.BeginAnimation(SolidColorBrush.ColorProperty, (AnimationTimeline)DS_Moonlight);
}
But DS_Moon returns Null!!
I just realized that i can do this:
Moon.Effect.BeginAnimation(DropShadowEffect.ColorProperty, (AnimationTimeline)DS_Moonlight);
I have a pointing to null error but i dont see where the problem is since everything gets initalised before used. the points where the error acures i have given a blockquote. Like always im thankfull for every help.
Button topAddBut = null;
//Button botAddBut = null;
Button loadBut = null;
Button saveBut = null;
StackPanel topSP = null;
//StackPanel botSP = null;
public MainWindow()
{
InitializeComponent();
loadBut = new Button { Content = "Load", Width = 70, Height = 23 };
Canvas.SetRight(loadBut, 160);
Canvas.SetBottom(loadBut, 24);
canvas1.Children.Add(loadBut);
saveBut = new Button { Content = "Save", Width = 70, Height = 23 };
Canvas.SetRight(saveBut, 80);
Canvas.SetBottom(saveBut, 24);
canvas1.Children.Add(saveBut);
StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
Canvas.SetLeft(topSP, 160);
Canvas.SetTop(topSP, 100);
AddWrapPanelTop();
AddTextBoxTop();
AddTopButton();
}
void AddTextBoxTop()
{
TextBox txtB1 = new TextBox();
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
WrapPanel wp = (WrapPanel)topSP.Children[0];
wp.Children.Add(txtB1);
}
void AddWrapPanelTop()
{
WrapPanel myWrapPanel = new WrapPanel();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);
myWrapPanel.Background = System.Windows.Media.Brushes.Magenta;
myWrapPanel.Orientation = Orientation.Horizontal;
myWrapPanel.Width = 4000;
myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
myWrapPanel.VerticalAlignment = VerticalAlignment.Top;
topSP.Children.Add(myWrapPanel);
}
void AddTopButton()
{
TextBox txtB1 = new TextBox();
txtB1.Background = System.Windows.Media.Brushes.Magenta;
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
topAddBut = new Button();
topAddBut.Click += new RoutedEventHandler(this.TopClick);
topAddBut.Content = "Add";
topAddBut.Width = 75;
// Add the buttons to the parent WrapPanel using the Children.Add method.
WrapPanel wp = (WrapPanel)topSP.Children[0];
wp.Children.Add(txtB1);
wp.Children.Add(loadBut);
this.topSP.Children.Add(wp);
}
void TopClick(Object sender, EventArgs e)
{
TextBox txtB1 = new TextBox();
txtB1.Background = System.Windows.Media.Brushes.Magenta;
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
Button s = (Button)sender;
WrapPanel wp = (WrapPanel)s.Parent;
wp.Children.Remove(s);
wp.Children.Add(txtB1);
AddTopButton();
// Add the buttons to the parent WrapPanel using the Children.Add method.
}
}
}
You define the following:
StackPanel topSP = null;
Then you have
StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
This is in your local scope though so will be destroyed when you exit the function
Finally you have
topSP.Children.Add(myWrapPanel);
This will still be set to null which is why your error occurs. Basically you are creating a local version of the same variable.
In order to resolve simply change the second definition to:
topSP = new StackPanel { Width = 400, Height = 50 };