WPF Animate double property changes - wpf

I'm trying to find a generic way to run an animation each time a property of type double is updated.
This has to be single solution which works for all double values. It means that I don't want to write a proprietory AttachedProperty for each UIElement property (One for Opacity and then another one for Height).
A pseudo-example of what I would like to accomplish:
<TextBlock x:Name="pageTitle" Text="title example" attached:AnimatedPropertyPath="(UIElement.Opacity)" Opacity="{Binding Path=TitleOpacity}" />
The attached property should listen to any change in opacity, cancel it and instead run an animation which makes it change gradually.
My question:
Does this exact syntax make sense? is it doable?
Is there a way to cancel the Opacity property immediate change by the binding and run the animation instead?
Any links to examples would be highly appreciated as I couldn't find any myself.
I want to avoid using DataTriggers because it requires too much xaml. It would be best embedded as an attached property exactly like the peudo xaml above.

My question:
Does this exact syntax make sense? is it doable?
Does it have to be an Attached Property? Would you be fine with using a Behavior?
Is there a way to cancel the Opacity property immediate change by the binding and run the animation instead?
Maybe with some hacks(not that I know of any). Again is this is an absolute must to have to intercept and cancel a normal DP action?
Any links to examples would be highly appreciated as I couldn't find any myself.
Well if you can tweak your requirement slightly, I can give you an example:
So if your requirement is to animate any double DP when it's bound-to value changes, we can do it with a Behavior
public class AnimateBehavior : Behavior<UIElement> {
public static readonly DependencyProperty ToAnimateProperty =
DependencyProperty.Register("ToAnimate", typeof(DependencyProperty),
typeof(AnimateBehavior), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double),
typeof(AnimateBehavior),
new FrameworkPropertyMetadata(0.0d, FrameworkPropertyMetadataOptions.None, ValueChangedCallback));
public DependencyProperty ToAnimate {
get { return (DependencyProperty) GetValue(ToAnimateProperty); }
set { SetValue(ToAnimateProperty, value); }
}
public double Value {
get { return (double) GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var item = d as AnimateBehavior;
if (item == null || item.AssociatedObject == null) {
return;
}
var newAnimation = new DoubleAnimation((double) e.NewValue, new Duration(new TimeSpan(0, 0, 1)));
item.AssociatedObject.BeginAnimation(item.ToAnimate, newAnimation);
}
}
Now in xaml:
<TextBlock Text="Hello">
<i:Interaction.Behaviors>
<local:AnimateBehavior ToAnimate="{x:Static TextBlock.OpacityProperty}" Value="{Binding ValueYouWantToBindToOpacity}" />
</i:Interaction.Behaviors>
</TextBlock>
Now with this approach you can animate any DP of that control that has a double type value. Like Opacity, FontSize ...
Main difference here to your original requirement is we do not bind the Value to the element. We instead have it bound to the Behavior. Now when this changes, we detect it in the behavior and via the AssociatedObject property of the behavior, apply the animation on the actual item.
We also satisfy your requirement to satisfy multiple double DP types by providing the property to animate when value changes via a DP to the behavior.
if you want to go even more generic, you can ofc make the Behavior accept a duration and type of animation too to have it even more generic.
Alternate for DP identifying property:
if you absolutely want to pass in "Opacity" and not the DP, then try something like this:
public static readonly DependencyProperty ToAnimateProperty =
DependencyProperty.Register("ToAnimate", typeof(PropertyPath),
typeof(AnimateBehavior), new FrameworkPropertyMetadata(null));
public PropertyPath ToAnimate
{
get { return (PropertyPath)GetValue(ToAnimateProperty); }
set { SetValue(ToAnimateProperty, value); }
}
so we made ToAnimate a PropertyPath
and in the ValueChanged function
private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var item = d as AnimateBehavior;
if (item == null || item.AssociatedObject == null) {
return;
}
var sb = new Storyboard();
var newAnimation = new DoubleAnimation((double) e.NewValue, new Duration(new TimeSpan(0, 0, 1)));
Storyboard.SetTarget(newAnimation, item.AssociatedObject);
Storyboard.SetTargetProperty(newAnimation, item.ToAnimate);
sb.Children.Add(newAnimation);
sb.Begin();
}
we create a Storyboard and use the PropertyPath with this you can have:
<TextBlock Text="Hello">
<i:Interaction.Behaviors>
<local:AnimateBehavior ToAnimate="Opacity" Value="{Binding ValueYouWantToBindToOpacity}" />
</i:Interaction.Behaviors>
</TextBlock>
I'd still prefer the DP over this method.

Related

Binding combobox ItemsSource only when opening

I'm trying to implement a ConnectionString dialog, where the user can enter all the values which are necessary to create a valid ConnectionString, e.g. UserID, IntegratedSecurity, etc....
There is also a ComboBox which enlists all the available databases which can be found at this endpoint. This ComboBox should only bind to the ItemsSource, when it is opened and not when the user changes for example the UserID.
Is there a easy way to refresh the ItemsSource values only when the Values are displayed(for example when opening the combobox). The Problem is when the user enters invalid values there is always an exception because the user has not finished entering all the necessary values.
I have already tried to implement this with the event ComboBox_DropDownOpened but I wonder if there is a more practical way to achieve this. I have noticed there is a BindingProperty "UpdateSourceTrigger" but I don't know if I can use it for my problem.
Thanks for any help!
<ComboBox Text="{Binding InitialCatalog}"
SelectedValue="{Binding InitialCatalog}"
ItemsSource="{Binding Databases}" IsEditable="True"/>
If the event ComboBox_DropDownOpened is working you can wrap it in a behavior which should look like this :
internal class ItemsSourceBindingOnOpenBehavior
{
public static readonly DependencyProperty SourceProperty =
DependencyProperty.RegisterAttached("Source", typeof(ObservableCollection<string>),
typeof(ItemsSourceBindingOnOpenBehavior),
new UIPropertyMetadata(null, OnSourceChanged));
public static ObservableCollection<string> GetSource(DependencyObject obj)
{
return (ObservableCollection<string>)obj.GetValue(SourceProperty);
}
public static void SetSource(DependencyObject obj, ObservableCollection<string> value)
{
obj.SetValue(SourceProperty, value);
}
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SetSource(d);
}
private static void SetSource(DependencyObject d)
{
var cbo = d as ComboBox;
if (cbo != null) cbo.DropDownOpened += (s, a) => { cbo.ItemsSource = GetSource(cbo); };
}
}
To activate the behavior use the two provided attached properties in your XAML :
<ComboBox a:ItemsSourceBindingOnOpenBehavior.Source="{Binding ViewModelCollection}"/>

cannot get attached property value

Hi I hope I can find some help here...
I am creating a WPF application using prism and MVVM.
I am trying to create an attached property which i found here.
in my ViewModel I get the focused Element by
var control = Keyboard.FocusedElement;
then I do
string value = ExtraTextBehaviourObject.GetExtraText(control as UIElement);
but the value returned is always null... Can anyone point me to the right direction???
UPDATE
public class ExtraTextBehaviourObject : DependencyObject
{
//Declare the dependency property
public static readonly DependencyProperty ExtraTextProperty;
static ExtraTextBehaviourObject()
{
//register it as attached property
ExtraTextProperty = DependencyProperty.RegisterAttached("ExtraText", typeof(string),
typeof(ExtraTextBehaviourObject));
}
//static function for setting the text
public static void SetExtraText(UIElement uiElement, string value)
{
if (uiElement != null)
{
uiElement.SetValue(ExtraTextProperty, value);
}
}
//static function for getting the text
public static string GetExtraText(UIElement uiElement)
{
if (uiElement != null)
{
return (string)uiElement.GetValue(ExtraTextProperty);
}
return "";
}
}
Set code in XAML
<dxe:TextEdit Text="{Binding Path=Customer.Comments, Mode=TwoWay}" AcceptsReturn="True" VerticalContentAlignment="Top"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Behaviors:ExtraTextBehaviourObject.ExtraText="HelloExtraText"
ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
The problem was that I wasn't getting the right control when I used Keyboard.FocusedElement. This may be a devexpress thing as I use their controls. So traversing the element tree upwards until I get that control solved my problem... Thank you Clemens!
Just an additional idea: Instead of traversing the tree by hand yourself, you could let WPF do that for you. Flag your attached property with FrameworkPropertyMetadataOptions.Inherits now the value can be retrieved by all children of the initial control where you set the attached property.
For example
<Grid MyService.MyProperty="True">
<TextBox .../>
var txt = aSender as TextBox;
var val = MyService.GetMyProperty(txt);
With Inherits GetMyProperty will return true, because it "inherits" the value from his parent Grid, without inherit of course the value would be false (not null, because its a value type).
DataContext for example is also an inherited dependency property.

Force Propagation of Coerced Value

tl;dr: Coerced values are not propagated across data bindings. How can I force the update across the data binding when code-behind doesn't know the other side of the binding?
I'm using a CoerceValueCallback on a WPF dependency property and I'm stuck at the issue that coerced values don't get propagated through to bindings.
Window1.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace CoerceValueTest
{
public class SomeControl : UserControl
{
public SomeControl()
{
StackPanel sp = new StackPanel();
Button bUp = new Button();
bUp.Content = "+";
bUp.Click += delegate(object sender, RoutedEventArgs e) {
Value += 2;
};
Button bDown = new Button();
bDown.Content = "-";
bDown.Click += delegate(object sender, RoutedEventArgs e) {
Value -= 2;
};
TextBlock tbValue = new TextBlock();
tbValue.SetBinding(TextBlock.TextProperty,
new Binding("Value") {
Source = this
});
sp.Children.Add(bUp);
sp.Children.Add(tbValue);
sp.Children.Add(bDown);
this.Content = sp;
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
typeof(int),
typeof(SomeControl),
new PropertyMetadata(0, ProcessValueChanged, CoerceValue));
private static object CoerceValue(DependencyObject d, object baseValue)
{
if ((int)baseValue % 2 == 0) {
return baseValue;
} else {
return DependencyProperty.UnsetValue;
}
}
private static void ProcessValueChanged(object source, DependencyPropertyChangedEventArgs e)
{
((SomeControl)source).ProcessValueChanged(e);
}
private void ProcessValueChanged(DependencyPropertyChangedEventArgs e)
{
OnValueChanged(EventArgs.Empty);
}
protected virtual void OnValueChanged(EventArgs e)
{
if (e == null) {
throw new ArgumentNullException("e");
}
if (ValueChanged != null) {
ValueChanged(this, e);
}
}
public event EventHandler ValueChanged;
public int Value {
get {
return (int)GetValue(ValueProperty);
}
set {
SetValue(ValueProperty, value);
}
}
}
public class SomeBiggerControl : UserControl
{
public SomeBiggerControl()
{
Border parent = new Border();
parent.BorderThickness = new Thickness(2);
parent.Margin = new Thickness(2);
parent.Padding = new Thickness(3);
parent.BorderBrush = Brushes.DarkRed;
SomeControl ctl = new SomeControl();
ctl.SetBinding(SomeControl.ValueProperty,
new Binding("Value") {
Source = this,
Mode = BindingMode.TwoWay
});
parent.Child = ctl;
this.Content = parent;
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
typeof(int),
typeof(SomeBiggerControl),
new PropertyMetadata(0));
public int Value {
get {
return (int)GetValue(ValueProperty);
}
set {
SetValue(ValueProperty, value);
}
}
}
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}
Window1.xaml
<Window x:Class="CoerceValueTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CoerceValueTest" Height="300" Width="300"
xmlns:local="clr-namespace:CoerceValueTest"
>
<StackPanel>
<local:SomeBiggerControl x:Name="sc"/>
<TextBox Text="{Binding Value, ElementName=sc, Mode=TwoWay}" Name="tb"/>
<Button Content=" "/>
</StackPanel>
</Window>
i.e. two user controls, one nested inside the other, and the outer one of those in a window. The inner user control has a Value dependency property that is bound to a Value dependency property of the outer control. In the window, a TextBox.Text property is bound to the Value property of the outer control.
The inner control has a CoerceValueCallback registered with its Value property whose effect is that this Value property can only be assigned even numbers.
Note that this code is simplified for demonstration purposes. The real version doesn't initialize anything in the constructor; the two controls actually have control templates that do everything that's done in the respective constructors here. That is, in the real code, the outer control doesn't know the inner control.
When writing an even number into the text box and changing the focus (e.g. by focusing the dummy button below the text box), both Value properties get duly updated. When writing an odd number into the text box, however, the Value property of the inner control doesn't change, while the Value property of the outer control, as well as the TextBox.Text property, show the odd number.
My question is: How can I force an update in the text box (and ideally also in the outer control's Value property, while we're at it)?
I have found an SO question on the same problem, but doesn't really provide a solution. It alludes to using a property changed event handler to reset the value, but as far as I can see, that would mean duplicating the evaluation code to the outer control ... which is not really viable, as my actual evaluation code relies on some information basically only known (without much effort) to the inner control.
Moreover, this blogpost suggests invoking UpdateTarget on the binding in TextBox.Text in the CoerceValueCallback, but first, as implied above, my inner control cannot possibly have any knowledge about the text box, and second, I would probably have to call UpdateSource first on the binding of the Value property of the inner control. I don't see where to do that, though, as within the CoerceValue method, the coerced value has not yet been set (so it's too early to update the binding), while in the case that the value is reset by CoerceValue, the property value will just remain what it was, hence a property changed callback will not get invoked (as also implied in this discussion).
One possible workaround I had thought of was replacing the dependency property in SomeControl with a conventional property and an INotifyPropertyChanged implementation (so I can manually trigger the PropertyChanged event even if the value has been coerced). However, this would mean that I cannot declare a binding on that property any more, so it's not a really useful solution.
I have been looking for an answer to this rather nasty bug myself for a while.
One way to do it, without the need to force an UpdateTarget on the bindings is this:
Remove your CoerceValue callback.
Shift the logic of the CoerceValue callback into your ProcessValueChanged callback.
Assign your coerced value to your Value property, when applicable (when the number is odd)
You will end up with the ProcessValueChanged callback being hit twice, but your coerced value will end up being effectively pushed to your binding.
Base on your code, your dependency property declaration would become this:
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value",
typeof(int),
typeof(SomeControl),
new PropertyMetadata(0, ProcessValueChanged, null));
And then, your ProcessValueChanged would become this:
private static void ProcessValueChanged(object source, DependencyPropertyChangedEventArgs e)
{
int baseValue = (int) e.NewValue;
SomeControl someControl = source as SomeControl;
if (baseValue % 2 != 0)
{
someControl.Value = DependencyProperty.UnsetValue;
}
else
{
someControl.ProcessValueChanged(e);
}
}
I slightly modified your logic, to prevent raising the event when the value needs to be coerced. As mentionned before, assigning to someControl.Value the coerced value will cause your ProcessValueChanged to be called twice in a row. Putting the else statement would only raise the events with valid values once.
I hope this helps!

ItemsControl ItemsSource lazy loading

Image you are creating a custom control behaving like ComboBox in WPF.
As a source of items you provide IQueryable<T> (or any kind of IEnumerable collection),
but you don't want to allow the control to call GetIterator() and iterate through it (some kind of a lazy loading).
Let's say you inherit from the (because you want all the funcionality of that control)
System.Windows.Controls.Primitives.Selector
class.
The Selector class inherits from System.Windows.Controls.ItemsControl class which provides a the well known dependency property ItemsSource.
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ItemsControl),
new FrameworkPropertyMetadata(null, new PropertyChangedCallback(ItemsControl.OnItemsSourceChanged)));
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ItemsControl control = (ItemsControl) d;
IEnumerable oldValue = (IEnumerable) e.OldValue;
IEnumerable newValue = (IEnumerable) e.NewValue;
ItemValueStorageField.ClearValue(d);
if ((e.NewValue == null) && !BindingOperations.IsDataBound(d, ItemsSourceProperty))
{
control.Items.ClearItemsSource();
}
else
{
control.Items.SetItemsSource(newValue); // PROBLEM
}
control.OnItemsSourceChanged(oldValue, newValue);
}
If I see it correctly, this is the place where it iterates.
internal void SetItemsSource(IEnumerable value)
{
if ((!this.IsUsingItemsSource && (this._internalView != null)) && (this._internalView.RawCount > 0))
{
throw new InvalidOperationException(SR.Get("CannotUseItemsSource"));
}
this._itemsSource = value;
this._isUsingItemsSource = true;
this.SetCollectionView(CollectionViewSource.GetDefaultCollectionView(this._itemsSource, this.ModelParent));
}
So I've decided to override metadata of ItemsSourceProperty and point it to my own static method,
where I'm planing not co call SetItemsSource (rather delay it).
How should it be done in your opinion?
Thank you
Your best bet would be to probably add a new dependency property, say DelayedItemsSource of type IEnumerable. Then you could bind ItemsSource to DelayedItemsSource after your delay.

DependencyProperty doesn't fire ValueChanged when new value is the same

Ok so here's the problem: I wrote a UserControl which receives a new value say like every 100ms and does something with it. It has to handle each new value setter, even if the value didn't change. The UserControl has a few DependencyProperties:
public double CurrentValue
{
get { return (double)GetValue(CurrentValueProperty); }
set { SetValue(CurrentValueProperty, value); }
}
public static readonly DependencyProperty CurrentValueProperty =
DependencyProperty.Register("CurrentValue", typeof(double), typeof(GraphControl), new UIPropertyMetadata(0d));
In the XAML where this control is used, I just set the Binding of CurrentValue to a (INotifyPropertyChanged-enabled) property:
<uc:UserControl CurrentValue="{Binding MyValue}" ... />
viewmodel:
public double MyValue
{
get { return _value; }
set
{
//if (_value == value) return;
_value= value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("MyValue"));
}
}
As you can see, I explicitly commented out the if equals then return so it will fire the PropertyChanged event even when the value gets updated to the same value.
Now back to my user control, I tried registering the ValueChanged in two ways; first by using the DependencyPropertyDescriptor:
var propertyDescriptor = DependencyPropertyDescriptor.FromProperty(CurrentValueProperty, typeof(GraphControl));
propertyDescriptor.AddValueChanged(this, OnCurrentValueChanged);
or by using the UIPropertyMetaData:
new UIPropertyMetadata(0d, OnCurrentValueChangedCallback)
so a stub of the callback would look like:
private void Callback(object sender, EventArgs e){
//do stuff
}
Ok now the problem is, the callback is not fired when the value doesn't explicitly change. I checked, and the PropertyChanged event is firing in my viewmodel, but the control doesn't see the change. When the value changes to a new value, it will handle the callback as expected.
Is there any way to override this behavior so that my callback method will always get hit?
EDIT:
I also tried using the CoerceValueCallback, and that one is hit when the value stays the same, but it doesn't help me with my problem I think...
You can wrap your value up in an object, i.e. create a class to hold it - then set the property to a new instance of that class containing the new value, every time. That means you're creating ~10 objects per second, but they are each different, will trigger the change event, and are only small (will be GC'd anyway). :)
Another alternative is to switch the value temporarily to something else then restore the previous one. You can do this entire trick transparently in the Coerce callback as such:
public static readonly DependencyProperty TestProperty = DependencyProperty.Register(
"Test", typeof(object), typeof(School),
new PropertyMetadata(null, TestChangedCallback, TestCoerceCallback));
static object TestCoerceCallback(DependencyObject d, object baseValue)
{
if (baseValue != null && (d.GetValue(TestProperty) == baseValue))
{
d.SetCurrentValue(TestProperty, null);
d.SetCurrentValue(TestProperty, baseValue);
}
return baseValue;
}
Just make sure your property code can handle the null value case gracefully.

Resources