How to smoothly animate a bound property value from it's old value to it's new value in WPF2019 Community Moderator ElectionChange the coordinate system of a Canvas in WPFWPF MVVM Property Change AnimationXAML animation to bound propertyBind a Line position to the position of an element in a usercontrolHow can i draw in a canvas from listIn XAML, How to call a function in timer, if it's input and output are bound to different objects?WPF Smoothly animated ProgressBar in templateCannot animate grid from previous value to a new valueWPF / C#: Connect shapes dynamically with linesCan you get the final value from an animation before that animation has completed?
Official degrees of earth’s rotation per day
Fastest way to pop N items from a large dict
What is a ^ b and (a & b) << 1?
Why did it take so long to abandon sail after steamships were demonstrated?
Is it insecure to send a password in a `curl` command?
et qui - how do you really understand that kind of phraseology?
How well should I expect Adam to work?
If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?
Violin - Can double stops be played when the strings are not next to each other?
Professor being mistaken for a grad student
combinatorics floor summation
How to deal with taxi scam when on vacation?
What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?
I am confused as to how the inverse of a certain function is found.
Book about superhumans hiding among normal humans
How to plot polar formed complex numbers?
Why no Iridium-level flares from other satellites?
Why does a Star of David appear at a rally with Francisco Franco?
Min function accepting varying number of arguments in C++17
Is a party consisting of only a bard, a cleric, and a warlock functional long-term?
Do I need life insurance if I can cover my own funeral costs?
A single argument pattern definition applies to multiple-argument patterns?
Is honey really a supersaturated solution? Does heating to un-crystalize redissolve it or melt it?
How are passwords stolen from companies if they only store hashes?
How to smoothly animate a bound property value from it's old value to it's new value in WPF
2019 Community Moderator ElectionChange the coordinate system of a Canvas in WPFWPF MVVM Property Change AnimationXAML animation to bound propertyBind a Line position to the position of an element in a usercontrolHow can i draw in a canvas from listIn XAML, How to call a function in timer, if it's input and output are bound to different objects?WPF Smoothly animated ProgressBar in templateCannot animate grid from previous value to a new valueWPF / C#: Connect shapes dynamically with linesCan you get the final value from an animation before that animation has completed?
How do I smoothly animate from the previous value of a bound property to it's new value?
Let's say we have the following Canvas and Line.
<Canvas>
<Line
Canvas.Top="0"
Stroke="#887FFF00"
StrokeThickness="2"
X1="0" Y1="0"
X2="0" Y2="Binding ActualHeight, RelativeSource=RelativeSource AncestorType=Canvas"
Canvas.Left="Binding Position"
>
</Line>
</Canvas>
The horizontal position of the line is determined by a Position property bound to the Canvas.Left attached property. When the Position changes from say, 100 to 200, I would like to animate the position of the line from it's previous value, smoothly to it's new value.
How do I do this?
wpf xaml
add a comment |
How do I smoothly animate from the previous value of a bound property to it's new value?
Let's say we have the following Canvas and Line.
<Canvas>
<Line
Canvas.Top="0"
Stroke="#887FFF00"
StrokeThickness="2"
X1="0" Y1="0"
X2="0" Y2="Binding ActualHeight, RelativeSource=RelativeSource AncestorType=Canvas"
Canvas.Left="Binding Position"
>
</Line>
</Canvas>
The horizontal position of the line is determined by a Position property bound to the Canvas.Left attached property. When the Position changes from say, 100 to 200, I would like to animate the position of the line from it's previous value, smoothly to it's new value.
How do I do this?
wpf xaml
add a comment |
How do I smoothly animate from the previous value of a bound property to it's new value?
Let's say we have the following Canvas and Line.
<Canvas>
<Line
Canvas.Top="0"
Stroke="#887FFF00"
StrokeThickness="2"
X1="0" Y1="0"
X2="0" Y2="Binding ActualHeight, RelativeSource=RelativeSource AncestorType=Canvas"
Canvas.Left="Binding Position"
>
</Line>
</Canvas>
The horizontal position of the line is determined by a Position property bound to the Canvas.Left attached property. When the Position changes from say, 100 to 200, I would like to animate the position of the line from it's previous value, smoothly to it's new value.
How do I do this?
wpf xaml
How do I smoothly animate from the previous value of a bound property to it's new value?
Let's say we have the following Canvas and Line.
<Canvas>
<Line
Canvas.Top="0"
Stroke="#887FFF00"
StrokeThickness="2"
X1="0" Y1="0"
X2="0" Y2="Binding ActualHeight, RelativeSource=RelativeSource AncestorType=Canvas"
Canvas.Left="Binding Position"
>
</Line>
</Canvas>
The horizontal position of the line is determined by a Position property bound to the Canvas.Left attached property. When the Position changes from say, 100 to 200, I would like to animate the position of the line from it's previous value, smoothly to it's new value.
How do I do this?
wpf xaml
wpf xaml
asked Mar 6 at 20:40
fractorfractor
460316
460316
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
By far the simplest way to animate a property smoothly in wpf is to use an animation.
There are a few tricky bits to animations for your requirement though.
You can't bind a "from" or "to" on an animation so this will involve creating an animation in code.
You need a dependency property and hence a dependency object to animate a value.
You could consider making the viewmodel which exposes position into a dependency object. You can then create a new animation in code when you need to animate it and just retain your binding there.
Or... you could define a storyboard as a resource and alter that in code to do your animation.
Like this https://social.technet.microsoft.com/wiki/contents/articles/31191.wpf-tips-animating-a-viewmodel.aspx
Maybe you don't fancy making the viewmodel a dependency object.
If you're ok with a bit of "code behind" in your view then that's a dependency object. You could alternatively add a dependency property to your window. Bind that to Position and then animate the canvas.left of your line or another dependency property the line is bound to.
add a comment |
Instead of binding to directly the source property using the Binding syntax in XAML, you could subscribe to the PropertyChanged event of the view model yourself in the view and animate the property programmatically, e.g.:
private void OnViewLoaded(object sender, RoutedEventArgs e)
ViewModel viewModel = DataContext as ViewModel;
if (viewModel != null)
Canvas.SetLeft(line, viewModel.Position);
viewModel.PropertyChanged += OnPropertyChanged;
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
if (e.PropertyName == "Position")
double from = Canvas.GetLeft(line);
if (double.IsNaN(from))
from = 0;
ViewModel viewModel = sender as ViewModel;
if (viewModel != null)
DoubleAnimation doubleAnimation = new DoubleAnimation()
From = from,
To = viewModel.Position,
Duration = TimeSpan.FromSeconds(1)
;
line.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
XAML:
<Canvas Width="100" Height="100" Background="Beige">
<Line x:Name="line"
Canvas.Top="0"
Stroke="#887FFF00"
StrokeThickness="2"
X1="0" Y1="0"
X2="0" Y2="Binding ActualHeight, RelativeSource=RelativeSource AncestorType=Canvas">
</Line>
</Canvas>
This is an example of a situation where you want to implement custom view-specific logic in the view, and you don't want to do this in XAML. This does not break the MVVM pattern in any way.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031803%2fhow-to-smoothly-animate-a-bound-property-value-from-its-old-value-to-its-new-v%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
By far the simplest way to animate a property smoothly in wpf is to use an animation.
There are a few tricky bits to animations for your requirement though.
You can't bind a "from" or "to" on an animation so this will involve creating an animation in code.
You need a dependency property and hence a dependency object to animate a value.
You could consider making the viewmodel which exposes position into a dependency object. You can then create a new animation in code when you need to animate it and just retain your binding there.
Or... you could define a storyboard as a resource and alter that in code to do your animation.
Like this https://social.technet.microsoft.com/wiki/contents/articles/31191.wpf-tips-animating-a-viewmodel.aspx
Maybe you don't fancy making the viewmodel a dependency object.
If you're ok with a bit of "code behind" in your view then that's a dependency object. You could alternatively add a dependency property to your window. Bind that to Position and then animate the canvas.left of your line or another dependency property the line is bound to.
add a comment |
By far the simplest way to animate a property smoothly in wpf is to use an animation.
There are a few tricky bits to animations for your requirement though.
You can't bind a "from" or "to" on an animation so this will involve creating an animation in code.
You need a dependency property and hence a dependency object to animate a value.
You could consider making the viewmodel which exposes position into a dependency object. You can then create a new animation in code when you need to animate it and just retain your binding there.
Or... you could define a storyboard as a resource and alter that in code to do your animation.
Like this https://social.technet.microsoft.com/wiki/contents/articles/31191.wpf-tips-animating-a-viewmodel.aspx
Maybe you don't fancy making the viewmodel a dependency object.
If you're ok with a bit of "code behind" in your view then that's a dependency object. You could alternatively add a dependency property to your window. Bind that to Position and then animate the canvas.left of your line or another dependency property the line is bound to.
add a comment |
By far the simplest way to animate a property smoothly in wpf is to use an animation.
There are a few tricky bits to animations for your requirement though.
You can't bind a "from" or "to" on an animation so this will involve creating an animation in code.
You need a dependency property and hence a dependency object to animate a value.
You could consider making the viewmodel which exposes position into a dependency object. You can then create a new animation in code when you need to animate it and just retain your binding there.
Or... you could define a storyboard as a resource and alter that in code to do your animation.
Like this https://social.technet.microsoft.com/wiki/contents/articles/31191.wpf-tips-animating-a-viewmodel.aspx
Maybe you don't fancy making the viewmodel a dependency object.
If you're ok with a bit of "code behind" in your view then that's a dependency object. You could alternatively add a dependency property to your window. Bind that to Position and then animate the canvas.left of your line or another dependency property the line is bound to.
By far the simplest way to animate a property smoothly in wpf is to use an animation.
There are a few tricky bits to animations for your requirement though.
You can't bind a "from" or "to" on an animation so this will involve creating an animation in code.
You need a dependency property and hence a dependency object to animate a value.
You could consider making the viewmodel which exposes position into a dependency object. You can then create a new animation in code when you need to animate it and just retain your binding there.
Or... you could define a storyboard as a resource and alter that in code to do your animation.
Like this https://social.technet.microsoft.com/wiki/contents/articles/31191.wpf-tips-animating-a-viewmodel.aspx
Maybe you don't fancy making the viewmodel a dependency object.
If you're ok with a bit of "code behind" in your view then that's a dependency object. You could alternatively add a dependency property to your window. Bind that to Position and then animate the canvas.left of your line or another dependency property the line is bound to.
answered Mar 6 at 20:58
AndyAndy
3,4621108
3,4621108
add a comment |
add a comment |
Instead of binding to directly the source property using the Binding syntax in XAML, you could subscribe to the PropertyChanged event of the view model yourself in the view and animate the property programmatically, e.g.:
private void OnViewLoaded(object sender, RoutedEventArgs e)
ViewModel viewModel = DataContext as ViewModel;
if (viewModel != null)
Canvas.SetLeft(line, viewModel.Position);
viewModel.PropertyChanged += OnPropertyChanged;
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
if (e.PropertyName == "Position")
double from = Canvas.GetLeft(line);
if (double.IsNaN(from))
from = 0;
ViewModel viewModel = sender as ViewModel;
if (viewModel != null)
DoubleAnimation doubleAnimation = new DoubleAnimation()
From = from,
To = viewModel.Position,
Duration = TimeSpan.FromSeconds(1)
;
line.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
XAML:
<Canvas Width="100" Height="100" Background="Beige">
<Line x:Name="line"
Canvas.Top="0"
Stroke="#887FFF00"
StrokeThickness="2"
X1="0" Y1="0"
X2="0" Y2="Binding ActualHeight, RelativeSource=RelativeSource AncestorType=Canvas">
</Line>
</Canvas>
This is an example of a situation where you want to implement custom view-specific logic in the view, and you don't want to do this in XAML. This does not break the MVVM pattern in any way.
add a comment |
Instead of binding to directly the source property using the Binding syntax in XAML, you could subscribe to the PropertyChanged event of the view model yourself in the view and animate the property programmatically, e.g.:
private void OnViewLoaded(object sender, RoutedEventArgs e)
ViewModel viewModel = DataContext as ViewModel;
if (viewModel != null)
Canvas.SetLeft(line, viewModel.Position);
viewModel.PropertyChanged += OnPropertyChanged;
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
if (e.PropertyName == "Position")
double from = Canvas.GetLeft(line);
if (double.IsNaN(from))
from = 0;
ViewModel viewModel = sender as ViewModel;
if (viewModel != null)
DoubleAnimation doubleAnimation = new DoubleAnimation()
From = from,
To = viewModel.Position,
Duration = TimeSpan.FromSeconds(1)
;
line.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
XAML:
<Canvas Width="100" Height="100" Background="Beige">
<Line x:Name="line"
Canvas.Top="0"
Stroke="#887FFF00"
StrokeThickness="2"
X1="0" Y1="0"
X2="0" Y2="Binding ActualHeight, RelativeSource=RelativeSource AncestorType=Canvas">
</Line>
</Canvas>
This is an example of a situation where you want to implement custom view-specific logic in the view, and you don't want to do this in XAML. This does not break the MVVM pattern in any way.
add a comment |
Instead of binding to directly the source property using the Binding syntax in XAML, you could subscribe to the PropertyChanged event of the view model yourself in the view and animate the property programmatically, e.g.:
private void OnViewLoaded(object sender, RoutedEventArgs e)
ViewModel viewModel = DataContext as ViewModel;
if (viewModel != null)
Canvas.SetLeft(line, viewModel.Position);
viewModel.PropertyChanged += OnPropertyChanged;
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
if (e.PropertyName == "Position")
double from = Canvas.GetLeft(line);
if (double.IsNaN(from))
from = 0;
ViewModel viewModel = sender as ViewModel;
if (viewModel != null)
DoubleAnimation doubleAnimation = new DoubleAnimation()
From = from,
To = viewModel.Position,
Duration = TimeSpan.FromSeconds(1)
;
line.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
XAML:
<Canvas Width="100" Height="100" Background="Beige">
<Line x:Name="line"
Canvas.Top="0"
Stroke="#887FFF00"
StrokeThickness="2"
X1="0" Y1="0"
X2="0" Y2="Binding ActualHeight, RelativeSource=RelativeSource AncestorType=Canvas">
</Line>
</Canvas>
This is an example of a situation where you want to implement custom view-specific logic in the view, and you don't want to do this in XAML. This does not break the MVVM pattern in any way.
Instead of binding to directly the source property using the Binding syntax in XAML, you could subscribe to the PropertyChanged event of the view model yourself in the view and animate the property programmatically, e.g.:
private void OnViewLoaded(object sender, RoutedEventArgs e)
ViewModel viewModel = DataContext as ViewModel;
if (viewModel != null)
Canvas.SetLeft(line, viewModel.Position);
viewModel.PropertyChanged += OnPropertyChanged;
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
if (e.PropertyName == "Position")
double from = Canvas.GetLeft(line);
if (double.IsNaN(from))
from = 0;
ViewModel viewModel = sender as ViewModel;
if (viewModel != null)
DoubleAnimation doubleAnimation = new DoubleAnimation()
From = from,
To = viewModel.Position,
Duration = TimeSpan.FromSeconds(1)
;
line.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
XAML:
<Canvas Width="100" Height="100" Background="Beige">
<Line x:Name="line"
Canvas.Top="0"
Stroke="#887FFF00"
StrokeThickness="2"
X1="0" Y1="0"
X2="0" Y2="Binding ActualHeight, RelativeSource=RelativeSource AncestorType=Canvas">
</Line>
</Canvas>
This is an example of a situation where you want to implement custom view-specific logic in the view, and you don't want to do this in XAML. This does not break the MVVM pattern in any way.
answered Mar 8 at 14:32
mm8mm8
86.5k81933
86.5k81933
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031803%2fhow-to-smoothly-animate-a-bound-property-value-from-its-old-value-to-its-new-v%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown