I want to change the background color of a rectangle to green for 1 second than change it back to black. I want to simulate a light being on or off - I dont want to fade the color in. The code below does what I want except it fades from black to green and vice versa. I definately dont want sleep on the UI thread..........
ColorAnimation animation = new ColorAnimation { From = Colors.Black, To = Colors.LightGreen, Duration = new Duration(TimeSpan.FromSeconds(1)), RepeatBehavior= new RepeatBehavior(1), AutoReverse=true };
SolidColorBrush activityLight = new System.Windows.Media.SolidColorBrush(Colors.Black);
ActivityIndicator.Fill = activityLight;
this.RegisterName("activityLight", activityLight);
ActivityStoryboard = new Storyboard();
ActivityStoryboard.Children.Add(animation);
Storyboard.SetTargetName(animation, "activityLight");
Storyboard.SetTargetProperty(animation, new PropertyPath(SolidColorBrush.ColorProperty));
You could use a ColorAnimationUsingKeyFrames:
var colorAnimation = new ColorAnimationUsingKeyFrames();
colorAnimation.KeyFrames.Add(
new DiscreteColorKeyFrame(Colors.Green, TimeSpan.FromSeconds(0d)));
colorAnimation.KeyFrames.Add(
new DiscreteColorKeyFrame(Colors.Black, TimeSpan.FromSeconds(1d)));
ActivityIndicator.Fill.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);
Related
I want to customize my combobox each item, same as font, background, etc.
Is there any simple way to set custom background color of each Combobox item?
This is simple example to do that. In this example my combobox has some item same as color name (Red, Blue, etc) and change the background of each item from this. Just flow the steps:
1) Set the DrawMode to OwnerDrawVariable:
If this property set to Normal this control never raise DrawItem event
ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
2) Add an DrawItem event:
ComboBox1.DrawItem += new DrawItemEventHandler(ComboBox1_DrawItem);
3) Entry your own code to customize each item:
private void ComboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = e.Bounds; //Rectangle of item
if (e.Index >= 0)
{
//Get item color name
string itemName = ((ComboBox)sender).Items[e.Index].ToString();
//Get instance a font to draw item name with this style
Font itemFont = new Font("Arial", 9, FontStyle.Regular);
//Get instance color from item name
Color itemColor = Color.FromName(itemName);
//Get instance brush with Solid style to draw background
Brush brush = new SolidBrush(itemColor);
//Draw the item name
g.DrawString(itemName, itemFont, Brushes.Black, rect.X, rect.Top);
//Draw the background with my brush style and rectangle of item
g.FillRectangle(brush, rect.X, rect.Y, rect.Width, rect.Height);
}
}
4) Colors need to be added as well:
ComboBox1.Items.Add("Black");
ComboBox1.Items.Add("Blue");
ComboBox1.Items.Add("Lime");
ComboBox1.Items.Add("Cyan");
ComboBox1.Items.Add("Red");
ComboBox1.Items.Add("Fuchsia");
ComboBox1.Items.Add("Yellow");
ComboBox1.Items.Add("White");
ComboBox1.Items.Add("Navy");
ComboBox1.Items.Add("Green");
ComboBox1.Items.Add("Teal");
ComboBox1.Items.Add("Maroon");
ComboBox1.Items.Add("Purple");
ComboBox1.Items.Add("Olive");
ComboBox1.Items.Add("Gray");
You can change the rectangle size and position to draw your own item as you want.
I hope this post is useful.
I'm new at Silverlight and trying samples for things animating the opacity of an object programatically.
I've come up with the following code:
MapShape myTestShape= RadMapInformationLayer.Items[0] as MapShape;
SolidColorBrush brush = new SolidColorBrush();
brush.Color = Colors.Purple;
myTestShape.Fill = brush;
//create a duration object
Duration duration = new Duration(TimeSpan.FromSeconds(5));
//create the storyboard
Storyboard story = new Storyboard();
//create double animation
DoubleAnimation animation = new DoubleAnimation();
//set the duration property
animation.Duration = duration;
//set the from and too values
animation.From = 1.0;
animation.To = 0.0;
//add the duration to the storyboard
story.Duration = duration;
//now set the target of the animation
Storyboard.SetTarget(animation, myTestShape);
//set the target property of this object
Storyboard.SetTargetProperty(animation, new PropertyPath(UIElement.OpacityProperty));
//add the double animations to the story board
story.Children.Add(animation);
if (!LayoutRoot.Resources.Contains("story1"))
LayoutRoot.Resources.Add("story1", story);
story.Begin();
For the property path I've also tried:
1. new PropertyPath("(FrameworkElement.Opacity)")
2. new PropertyPath("(FrameworkElement.Opacity)")
3. new PropertyPath("(Control.Opacity)")
And a few others, I'm having zero luck with this.
Can anyone see where I'm going wrong?
Thanks,
Jacques
I have done databinding to MapShape.FillProperty before.
Try:
//create double animation
ColorAnimation animation = new ColorAnimation();
//set the duration property
animation.Duration = duration;
//set the from and too values
animation.From = Colors.Purple;
animation.To = Colors.Transparent;
//add the duration to the storyboard
story.Duration = duration;
//now set the target of the animation
Storyboard.SetTarget(animation, rect);
//set the target property of this object
Storyboard.SetTargetProperty(animation, new PropertyPath("(MapShape.Fill).Color"));
EDIT:
As for why your code is not working -- Looking through MapShape.SetShapeFillStroke(), it appears that Telerik won't bind the MapShape's Opacity to its inner primitive shape's Opacity unless you provide a fill. Do you have a Fill defined in XAML? If not, try providing one. Otherwise, maybe the code is defined too early in the Shape's lifecycle (in or after ReadCompleted)?
<telerik:InformationLayer x:Name="StateLayer">
<telerik:InformationLayer.Reader>
...
</telerik:InformationLayer.Reader>
<telerik:InformationLayer.ShapeFill>
<telerik:MapShapeFill Fill="{StaticResource CommonBackgroundLightBrush}" Stroke="#5A636B" StrokeThickness="1" />
</telerik:InformationLayer.ShapeFill>
I am having some trouble animating the items in my ListBox. Animations on the OpacityProperty work great but when i try to animate the position of my ListBoxItem it simply does not move an inch (No exceptions are thrown, not even a log message indicating error).
Here is the code i am using:
private void deletAnimation()
{
Todo todoToDelete = App.ViewModel.Todos[1];
Storyboard storyboard = new Storyboard();
DoubleAnimation alphaAnim = new DoubleAnimation();
alphaAnim.From = 1;
alphaAnim.To = 0;
alphaAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500));
ListBoxItem target = TodoList.ItemContainerGenerator.ContainerFromItem(todoToDelete) as ListBoxItem;
Storyboard.SetTarget(alphaAnim, target);
Storyboard.SetTargetProperty(alphaAnim, new PropertyPath(UIElement.OpacityProperty));
storyboard.Children.Add(alphaAnim);
for (int i = App.ViewModel.Todos.IndexOf(todoToDelete) + 1; i < App.ViewModel.Todos.Count; i++)
{
Todo todo = App.ViewModel.Todos[i];
target = TodoList.ItemContainerGenerator.ContainerFromItem(todo) as ListBoxItem;
DoubleAnimation translateAnim = new DoubleAnimation();
translateAnim.From = target.RenderTransformOrigin.Y;
translateAnim.To = target.RenderTransformOrigin.Y - target.ActualHeight;
translateAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500));
translateAnim.BeginTime = TimeSpan.FromMilliseconds(500);
Storyboard.SetTarget(translateAnim, target);
Storyboard.SetTargetProperty(translateAnim, new PropertyPath(TranslateTransform.YProperty));
storyboard.Children.Add(translateAnim);
}
storyboard.Begin();
}
Some things i have noticed while debugging:
The RenderTransformOrigin.Y property is always 0 no matter which ListBoxItem is referenced.
The Height property is NaN though the ActualHeight property is 67
The parent property of the ListBoxItem is null
These two things make me wonder if i am given a reference to a ListBoxItem that is not rendered of the screen? But as the Opacity animation works perfectly and all of my list is visible(only contains 3 items at the moment) i do not see how this could be the case.
I have also tried using a PointAnimation and animating the RenderTransformOrigin property directly but this gave the same result (nothing that is).
Any help is appreciated, thanks!
Please check out this link:
http://blogs.msdn.com/b/jasongin/archive/2011/01/03/wp7-reorderlistbox-improvements-rearrange-animations-and-more.aspx
Someone has created a class that you can easily incorporate into your app. This has animations for adding/removing items from your list.
This should save you a lot of time and frustration.
I'm doing a Surface Application.
And there I have something like a bulletin board where little cards with news on it are pinned on.
On click they shall fly out of the board and scale bigger.
My storyboard works well, except for the first time it runs. It's not a smooth animation then but it scales to its final size immediately and it's the same with the orientation-property. Just the center-property seems to behave correctly.
This is an example for one of my Storyboards doing that:
Storyboard stb = new Storyboard();
PointAnimation moveCenter = new PointAnimation();
DoubleAnimationUsingKeyFrames changeWidth = new DoubleAnimationUsingKeyFrames();
DoubleAnimationUsingKeyFrames changeHeight = new DoubleAnimationUsingKeyFrames();
DoubleAnimationUsingKeyFrames changeOrientation = new DoubleAnimationUsingKeyFrames();
moveCenter.From = News1.ActualCenter;
moveCenter.To = new Point(250, 400);
moveCenter.Duration = new Duration(TimeSpan.FromSeconds(1.0));
moveCenter.FillBehavior = FillBehavior.Stop;
stb.Children.Add(moveCenter);
Storyboard.SetTarget(moveCenter, News1);
Storyboard.SetTargetProperty(moveCenter, new PropertyPath(ScatterViewItem.CenterProperty));
changeWidth.Duration = TimeSpan.FromSeconds(1);
changeWidth.KeyFrames.Add(new EasingDoubleKeyFrame(266, KeyTime.FromTimeSpan(new System.TimeSpan(0, 0, 1))));
changeWidth.FillBehavior = FillBehavior.Stop;
stb.Children.Add(changeWidth);
Storyboard.SetTarget(changeWidth, News1);
Storyboard.SetTargetProperty(changeWidth, new PropertyPath(FrameworkElement.WidthProperty));
changeHeight.Duration = TimeSpan.FromSeconds(1);
changeHeight.KeyFrames.Add(new EasingDoubleKeyFrame(400, KeyTime.FromTimeSpan(new System.TimeSpan(0, 0, 1))));
changeHeight.FillBehavior = FillBehavior.Stop;
stb.Children.Add(changeHeight);
Storyboard.SetTarget(changeHeight, News1);
Storyboard.SetTargetProperty(changeHeight, new PropertyPath(FrameworkElement.HeightProperty));
changeOrientation.Duration = TimeSpan.FromSeconds(1);
changeOrientation.KeyFrames.Add(new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(new System.TimeSpan(0, 0, 1))));
changeOrientation.FillBehavior = FillBehavior.Stop;
stb.Children.Add(changeOrientation);
Storyboard.SetTarget(changeOrientation, News1);
Storyboard.SetTargetProperty(changeOrientation, new PropertyPath(ScatterViewItem.OrientationProperty));
stb.Begin(this);
News1.Center = new Point(250, 400);
News1.Orientation = 0;
News1.Width = 266;
News1.Height = 400;
Pin1.Visibility = Visibility.Collapsed;
news1IsOutside = true;
Scroll1.IsEnabled = true;
What's wrong with it?
The Problem
The essence of the problem is that you are calling stb.Begin() and then immediately changing News1.Width, etc. Unfortunately stb.Begin() is not guaranteed to start the animations immediately: At times it does so in a dispatcher callback.
What is happening to you is that the first time your storyboard executes, stb.Begin() schedules a dispatcher callback to start the animations and immediately returns. The next four lines of your code update the values:
News1.Center = new Point(250, 400);
News1.Orientation = 0;
News1.Width = 266;
News1.Height = 400;
When the animations actually start in the dispatcher callback they see the new values and use those as their starting values. This causes the object appears to jump to the new value immediately.
For example, changeWidth declares a keyframe that animates the value to 266:
changeWidth.KeyFrames.Add(new EasingDoubleKeyFrame(266, ...
And later the initial width is set to 266:
News1.Width = 266;
So if the storyboard is delayed starting, the width will animate from 266 to 266. In other words, it will not change. If you later use another animation to change News1.Width to something other than 266 and then run the changeWidth animation, it will work.
Your moveCenter animation works reliably because it actually sets its From value to the current value:
moveCenter.From = News1.ActualCenter;
moveCenter.To = new Point(250, 400);
Thus the animation always starts at the old center, even if the News1.Center = new Point(250,400) once the animation has started.
The Solution
Just as you set "From" on your PointAnimation, you can also set an initial value in your other animations. This is done by adding a key frame at time=0 specifying the current width:
changeWidth.Duration = TimeSpan.FromSeconds(1);
changeWidth.KeyFrames.Add(new DiscreteDoubleKeyframe(News1.Width, KeyTime.Paced));
changeWidth.KeyFrames.Add(new EasingDoubleKeyFrame(266, KeyTime.Paced));
This code uses the fact that KeyTime.Paced automatically results in 0% and 100% if there are two key frames. In fact, setting the first frame as KeyFrame.Paced will always be equivalent to KeyTime.FromTimeSpan(TimeSpan.Zero).
Another solution might be to use FillBehavior.HoldEnd, then hook up to the Storyboard.Completed event and:
Set the local values as you originally did
Call Storyboard.Remove
This would also have the advantage that the local values will be accessible from the properties.
I'm trying to create a simple toolbar in WPF, but the toolbar shows up with no corresponding buttons on it, just a very thin blank white strip. Any idea what I'm doing wrong, or what the recommended procedure is? Relevant code fragments so far:
var tb = new ToolBar();
var b = new Button();
b.Command = comback;
Image myImage = new Image();
myImage.Source = new BitmapImage(new Uri("back.png", UriKind.Relative));
b.Content = myImage;
tb.Items.Add(b);
var p = new DockPanel();
//DockPanel.SetDock(mainmenu, Dock.Top);
DockPanel.SetDock(tb, Dock.Top);
DockPanel.SetDock(sb, Dock.Bottom);
//p.Children.Add(mainmenu);
p.Children.Add(tb);
p.Children.Add(sb);
Content = p;
Without a third child-element for the Dockpanel p, the 'sb' element will fill everything except for the area of tb. The ToolBar will autoSize (its Height) according to its contents.
I suggest adding a simple text button first, to check the overall layout:
var b2 = new Button();
b2.Content = "B2";
tb.Items.Add(b2);
And then investigate what is wrong with the "back.png" image.